【问题标题】:How to make program keep track of min and max in java [closed]如何让程序在java中跟踪最小值和最大值[关闭]
【发布时间】:2015-12-30 17:32:08
【问题描述】:

这个程序需要输出最小应答数和最大应答数。似乎无法正确处理这一问题。最后,结果还需要包含“输入的最小数量是”和“输入的最大数量是”的结果

{
    public static void main(String[] args)
 {
    // display operational messages
    System.out.println("Please enter test scores that range from 0  to        100.");
    System.out.println("To end the program enter 999.");
    System.out.println();  // print a blank line

    // initialize variables and create a Scanner object
    double scoreTotal = 0;
    int scoreCount = 0;
    int testScore = 0;
    Scanner sc = new Scanner(System.in);

    //declaring what is min and what is max
    int x = 100;
    int y= 0; 
    int max = Math.max(x, y); // max is 100
    int min = Math.min(x, y); // min is 0

    // get a series of test scores from the user
    while (testScore != 999)
   '   enter code here`    {
        // get the input from the user
        System.out.print("Enter score: ");
        testScore = sc.nextInt();

        // accumulate score count and score total
        if (testScore <= 100)
        {
            scoreCount = scoreCount += 1;
            scoreTotal = scoreTotal += testScore;
        }
        else if (testScore != 999)
            System.out.println("Invalid entry, not counted");



    }

    // display the score count, score total, and average score
    double averageScore = scoreTotal / scoreCount;
    String message = "\n" +
                     "Score count:   " + scoreCount + "\n"
  `enter code here`                 + "Score total:   " + scoreTotal  +            "\n"
                   + "Average score: " + averageScore + "\n";
    System.out.println(message);

}

【问题讨论】:

  • 您从不尝试确定最小值/最大值?
  • Can't seem to get this one right - 难怪您不尝试它。请尝试并发布您尝试过的内容以及失败的原因。

标签: java max min


【解决方案1】:

对于:最后结果还需要包含“输入的最小数量是”和“输入的最大数量是”的结果

我建议将您的分数或输入的数字存储在列表(Arraylist)或任何其他集合中 然后使用java.util.Collections ma​​xmin 方法。

List<Integer>list=new ArrayList<Integer>();
Collections.max(list);
Collections.min(list);

【讨论】:

    【解决方案2】:

    一个简单的解决方案是在两个单独的整数中跟踪最低和最高分数,并在检查测试结果时对其进行修改。如果插入的测试分数大于我们当前拥有的最大值,则我们更新最大值以匹配分数。如果它低于我们目前拥有的最小值,那么我们更新最小值:

    ...
    int minScore = 100, maxScore = 0;
    while (testScore != 999)
       '   enter code here`    {
            // get the input from the user
            System.out.print("Enter score: ");
            testScore = sc.nextInt();
    
            // accumulate score count and score total
            if (testScore <= 100)
            {
                scoreCount = scoreCount += 1;
                scoreTotal = scoreTotal += testScore;
                maxScore = Math.max(maxScore, testScore);
                minScore = Math.min(minScore, testScore);
            }
            ...
    

    【讨论】:

    • 代码转储不会帮助 OP 学习。请至少简要说明您做了什么以及为什么。
    • 好的,我添加了一个解释:)
    • 非常感谢大家。
    猜你喜欢
    • 2012-07-19
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 2012-11-21
    • 2021-06-01
    • 2014-11-03
    相关资源
    最近更新 更多