如何从用户输入打印正确的最小值和最大值

时间:2018-01-29 22:03:39

标签: java max minimum

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

程序正在正确打印min,并且总和将先前生成的总和与新生成的总和相加。如何更改它以便为最小值提供正确的输出,以便停止将新的前缀添加到新的?任何帮助将不胜感激。

输出的图像链接显示最小输出问题。最小应该是66.0,但它说59.0。   output     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? ");
       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;
    }   
}

2 个答案:

答案 0 :(得分:0)

执行后只需清​​理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;
    }
}

答案 1 :(得分:0)

在继续之前,我绝对认为你应该阅读@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

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

最后,为了更改变量 max min ,您必须将新值与其当前值进行比较。例如,要更改 min 值,您必须将得分与当前 min 进行比较。

希望它有所帮助。