【问题标题】:How to print proper minimum and maximum from user input如何从用户输入中打印正确的最小值和最大值
【发布时间】:2018-07-08 16:59:27
【问题描述】:

我正在尝试编写一个程序,允许用户输入他们想要显示的多个随机等级以及生成列表的总和、平均值、最小值和最大值。分数范围从 60 - 100。

程序正在正确打印最小值,并且总和将先前生成的总和与新生成的总和相加。我怎样才能改变它,以便它给出正确的最小输出,从而停止将以前的总和添加到新的总和中?任何帮助将不胜感激。

输出的图片链接显示了最小的输出问题。最小值应该是 66.0,但它说的是 59.0。 output 导入 java.util.Scanner;

public class A03C
{
   public static void main(String[] args) 
   {
    Scanner input = new Scanner(System.in);

    System.out.print("How many scores? ");
    int howMany = input.nextInt();

    double score = 0;
    double sum = 0;
    double average = 0;
    double max = 0;
    double min = 60;

    while (howMany > 0)
    {
       for (int i = 1; i <= howMany; i++)
       {
        score = 60 + (int)(Math.random() * ((100 - 60) +1));

        if (letterGrade(score));

        sum += score++;

        average = (sum/howMany);

        if (score > max)
               max = score;

        if (score < max)
               min = score;

       }
       System.out.println("Sum: " + sum);
       System.out.println("Average: " + average);
       System.out.println("Max: " + (max - 1));
       System.out.println("Min: " + (min - 1));

       System.out.println("How many scores? ");
       howMany = input.nextInt();
    }

   }

    public static boolean letterGrade(double score)
    { 
       if (score >= 92.0) 
          System.out.println(score + " is an A");
       else if (score >= 83.0)
          System.out.println(score + " is a B"); 
       else if (score >= 75.0)
          System.out.println(score + " is a C");
       else 
          System.out.println(score + " is an F");
       return false;
    }   
}

【问题讨论】:

  • 看来你需要学习使用调试器了。请帮助自己一些complementary debugging techniques。如果您之后仍有问题,请随时回来提出更具体的问题。
  • “遇到麻烦”是一个含糊不清的问题描述。请edit您的问题包括您遇到的问题。你得到编译错误吗?运行时错误?输出与预期不同?等等。请不要忘记包含您遇到的任何错误的全文,以及描述不当行为时的预期实际输出。

标签: java max minimum


【解决方案1】:

执行后只清除 sum 变量,我也将 letterGrade 的返回值更改为 true:

import java.util.Scanner;

public class A03C {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("How many scores? ");
        int howMany = input.nextInt();

        double score = 0;
        double sum = 0;
        double average = 0;
        double max = 0;
        double min = 60;

        while (howMany > 0) {
            for (int i = 1; i <= howMany; i++) {
                score = 60 + (int) (Math.random() * ((100 - 60) + 1));

                if (letterGrade(score))
                    sum += score++;

                average = (sum / howMany);

                if (score > max)
                    max = score;

                if (score < max)
                    min = score;

            }
            System.out.println("Sum: " + sum);
            System.out.println("Average: " + average);
            System.out.println("Max: " + (max - 1));
            System.out.println("Min: " + (min - 1));

            System.out.println("How many scores? ");

            sum = 0;

            howMany = input.nextInt();
        }

    }

    public static boolean letterGrade(double score) {
        if (score >= 92.0)
            System.out.println(score + " is an A");
        else if (score >= 83.0)
            System.out.println(score + " is a B");
        else if (score >= 75.0)
            System.out.println(score + " is a C");
        else
            System.out.println(score + " is an F");
        return true;
    }
}

【讨论】:

    【解决方案2】:

    在继续之前,我绝对认为您应该阅读@joe C 的评论。 据我了解,您是初学者,我将解释我所做的更改。

    import java.util.Scanner;
    
    
    public class A03C
    {
       public static void main(String[] args) 
       {
        Scanner input = new Scanner(System.in);
    
        System.out.print("How many scores? ");
        int howMany = = input.nextInt();
    
        double score = 0;
        double sum = 0;
        double average = 0;
        double max = 60;
        double min = 100;
    
        while (howMany > 0)
        {
           for (int i = 1; i <= howMany; i++)
           {
            score = 60 + (int)(Math.random() * ((100 - 60) +1));
    
            letterGrade(score);
    
            sum += score++;
    
            average = (sum/howMany);
    
            if (score > max)
                   max = score;
    
            if (score < min)
                   min = score;
    
           }
           System.out.println("Sum: " + sum);
           System.out.println("Average: " + average);
           System.out.println("Max: " + (max - 1));
           System.out.println("Min: " + (min - 1));
    
           System.out.println("How many scores? ");
           howMany = input.nextInt();
        }
    
       }
    
        public static void letterGrade(double score)
        { 
           if (score >= 92.0) 
              System.out.println(score + " is an A");
           else if (score >= 83.0)
              System.out.println(score + " is a B"); 
           else if (score >= 75.0)
              System.out.println(score + " is a C");
           else 
              System.out.println(score + " is an F");
        }   
    }
    

    从函数 letterGrade 开始。由于该函数总是返回 false 并且只打印一个句子,因此您可以将 boolean 替换为 void

    你犯的另一个错误是,如果你想找到 maxmin,变量应该取你想要的倒数。因此变量 max 应取最小变量 (60) 和最大值 (100)。

    最后,为了更改变量 maxmin,您必须将新值与其当前值进行比较。也就是说,要更改 min 值,例如,您必须将 score 与当前 min 进行比较。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 2014-12-31
      • 1970-01-01
      • 2020-02-28
      • 2020-09-19
      • 2019-05-26
      相关资源
      最近更新 更多