繁体   English   中英

为什么此Java代码无法计算平均最大和最小?

[英]Why is this Java code not working calculate average max and min?

我应该构建一个程序,以获取班级的成绩并计算最小,最大和平均值,然后询问是否要继续。 这是我使用的代码,但仍然无法正常工作。 您能看到错误在哪里吗?

public class calculateAvgMAxMin
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in); // declares scanner input
        LinkedList<Integer> list = new LinkedList<Integer>(); // declare new LinkedList of variables which are the grades

        int answer = 1; // declare variable answer =1 
        do { // start of do loop 

            System.out.print("Please enter the name of the course:"); // gives a prompt to enter the name of the course

            // goes to the next line

            String course=input.nextLine(); // declare course as string and sets the value as the input

            System.out.println("Please enter the scores for " + course + " on a single line and type a -1 at the end"); //prompts user to enter scores for class in one line and -1 to stop

            int max=-100; // declare max as integer = -100
            int min=500;//declare min as integer = 500

            int sum=0;// declare sum as intgetr = 0
            int grade;     // declare grade as integer  

            grade=input.nextInt(); // set grade as next user iput

            if (grade==-1) //  if statement if the sentinel value is set then stop
                break;

            list.add(grade); // adds the grade from the grade variable to the LinkedList
            sum=sum+grade;// put the value of sum to sum and the new grade

            if (grade>max) // if statement if grade is more than the max then sets max as the grade
                max=grade;
            if (grade<min) // if statement if grade is less than the min then sets min as the grade
                min=grade;

            System.out.println("The course name: "+ course); // prints message of the course name entered
            System.out.println("Number of Scores :"+list.size()); // sits the number of scores using the size of the list
            System.out.printf("The Average Score: %.2f" ,(double)sum/list.size()); // calculates average to 2 decimal values using the sum divided by the length of the list
            System.out.println(); // goes to next line
            System.out.println("The Minimum Score: "+min); // sets the minimum value to min
            System.out.println("The Maximum Score :"+max);// sets the minimum value to min
            answer = JOptionPane.showConfirmDialog(null," Would you like to continue?"); // sets the value of the variable answer to confrim dialog box
        } while (answer == 0);  // sets the condition for do loop to answer == o so that it would continue only if the yes is selected

    }
}

我认为以下几行是不正确的:

int max=-100; // declare max as integer = -100
 int min=500;//declare min as integer = 500

他们应该是:

int max= 500; // declare max as integer = -100
 int min=-100;//declare min as integer = 500

另外,还应将它们移至do-while之外。

您需要为每个分数再做一次

     int grade;     // declare grade as integer  
     do{
        grade=input.nextInt(); // set grade as next user iput

        if(grade==-1) //  if statement if the sentinel value is set then stop
             break ;

         list.add(grade); // adds the grade from the grade variable to the LinkedList
         sum=sum+grade;// put the value of sum to sum and the new grade

         if(grade>max) // if statement if grade is more than the max then sets max as the grade
             max=grade;
         if(grade<min) // if statement if grade is less than the min then sets min as the grade
             min=grade;
     } while (true);

我将从编程到List接口(而不是具体的集合)开始,并且我更喜欢Diamond运算符<> (可用的Java 7+)。 接下来,我将maxmin分别初始化为Integer.MIN_VALUEInteger.MAX_VALUE (因为没有int小于第一个,或者随后的是大于第二个)。 然后,我更喜欢Math.max(int, int)Math.min(int, int) 最后,建议您在显示统计信息之前获取所有值。 就像是,

List<Integer> list = new LinkedList<>();
int answer = 1;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int sum = 0;
do {
    System.out.print("Please enter the name of the course:");
    String course = input.nextLine();
    System.out.printf("Please enter the scores for %s on a single "
            + "line and type a -1 at the end%n", course);
    int grade = input.nextInt();
    if (grade == -1) {
        break;
    }
    System.out.printf("Course %s, Grade %d%n", course, grade);
    list.add(grade);
    sum += grade;
    max = Math.max(max, grade);
    min = Math.min(min, grade);
    answer = JOptionPane.showConfirmDialog(null,
            "Would you like to continue?");
} while (answer == 0);
System.out.println("Number of Scores :" + list.size());
System.out.printf("The Average Score: %.2f%n",
        sum / (double) list.size());
System.out.println("The Minimum Score: " + min);
System.out.println("The Maximum Score :" + max);

您将最大值和最小值设置到何处以及设置了什么似乎是一个错误。 它应该是:

int min = -100;
int max = 500;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM