java代码不能像我期待的那样工作

时间:2022-05-05 17:13:57

I am really struggling on getting my java code to work. I have coded a program which determines what grade each score will get once entered in by the user and also finds the largest and smallest score. The program successfully figures out which score belongs in which grade but once I implement the piece of code which tries to find the largest number out of the scores it doesn't seem to work and I'm not sure what it is!

我真的很努力让我的java代码工作。我编写了一个程序,用于确定用户输入后每个分数的等级,并找出最大和最小分数。该程序成功地计算出哪个分数属于哪个等级,但是一旦我实现了那些试图找到分数中最大数字的代码,它似乎不起作用,我不确定它是什么!

here is the code...

这是代码......

import java.util.Scanner;

public class Grade
 {
 public static void main(String[] args)
 {
        int Agrade=0; //different variables used throughout the code
        int Bgrade=0;
        int Cgrade=0;
        int Dgrade=0;
        int Fgrade=0;
        int count=0;




    Scanner in = new Scanner(System.in);   //name of Scanner 

    System.out.println("Please enter the exam grades one after the other ");
    System.out.println("Please enter a negative number at the end of the grade list to control the flow and then press enter :");
    int score = in.nextInt(); //stores numbers inputted

    System.out.println("Please enter the grades again for the largest number: ");
    double largest = in.nextDouble();


    while (in.hasNextDouble()){
       double input = in.nextDouble();
       if (input > largest) {
          largest = input;
       }
    }



 while(score>0)   //start while loop
     {
       count++;
 if(score>=70 && score<=100)
        Agrade++;
    else if(score>=60 && score<=69)
        Bgrade++;
    else if(score>=50 && score<=59)
        Cgrade++;
    else if(score>=40 && score<=49)
        Dgrade++;
    else if(score>=0 && score<=39)
        Fgrade++;
    score = in.nextInt();
 } //end while



 System.out.println("Total number of grades :"+ count);
 System.out.println("The largest number :"+ largest);
 System.out.println("The number of As :"+ Agrade);
 System.out.println("The number of Bs :"+ Bgrade);
 System.out.println("The number of Cs :"+ Cgrade);
 System.out.println("The number of Ds :"+ Dgrade);
 System.out.println("The number of Fs :"+ Fgrade);

 } // end main
} // end class

Thank you!

3 个解决方案

#1


2  

That's probably an exercise so will give just the idea. Your while loop seems to be fine, just declare two variables before it. one for holding the max grade initialized with lowest number and another for min grade with highest number.

这可能是一个练习,所以会给出这个想法。你的while循环似乎很好,只需在它之前声明两个变量。一个用于保持最低等级初始化为最低编号,另一个用于最高等级,最高编号。

score = in.nextInt(); do

score = in.nextInt();做

if ( score > highest  ) { highest = score;}
if ( score < lowest ) { lowest = score }

good luck.

#2


0  

The problem is likely that you are declaring the variable input every time the while loop runs. Instead, declare the variable like this:

问题很可能是每次while循环运行时都声明变量输入。相反,声明变量如下:

double input;

and do it before the while loop. Then in the while loop replace that line with:

并在while循环之前完成。然后在while循环中用以下代码替换该行:

input = in.nextDouble();

If this isn't the problem, it might be how you've laid out the code. The second while loop, which begins with while (score>0) should be where you have the line int score = in.nextInt() Putting this after the first while loop means that it's executed after that while loop.

如果这不是问题,那么可能就是你如何布置代码。第二个while循环以while(score> 0)开头,应该是你的行int score = in.nextInt()在第一个while循环之后放置它意味着它在while循环之后执行。

As a side note, common variables like Agrade should be written in camelCase, in which the first letter is lowercase and the letter of the next word is uppercase. This would make your variables

作为旁注,像Agrade这样的常见变量应该写在camelCase中,其中第一个字母是小写字母,下一个字母的字母是大写字母。这会使你的变量

int aGrade = 0;
int bGrade = 0;

and so on. This is just proper form and shouldn't effect your program. Also, you should probably declare score and input at the top of your program since you use them in loops. This is also good form and organization. It would look like this:

等等。这只是正确的形式,不应该影响你的程序。此外,您应该在程序顶部声明得分和输入,因为您在循环中使用它们。这也是良好的形式和组织。它看起来像这样:

int score;
double input;

#3


-1  

Check comments on code to understanding.

检查代码注释以理解。

MainClass:

import java.util.Scanner;

public class Grade
{
    public static void main ( String [ ] args )
    {
        /**
         * This gradesCount represent the A,B,C,D,E,F counts
         */
        int[] gradesCount = {0,0,0,0,0,0}; 
        double inputScore = 0;  
        double bestScore = 0;
        int total = 0;

        @SuppressWarnings ( "resource" )
        Scanner in = new Scanner(System.in);

        System.out.println ( "If you want to end, enter a negative number" );
        System.out.print("Enter score: ");

        do
        {
            inputScore = in.nextDouble ( );
            //validation for the first iteration
            if(inputScore < 0 || inputScore > 100) break;
            System.out.print("Enter score: ");
            //Add to corresponding grade count
            if(inputScore>=70 && inputScore<=100) gradesCount[0]++;
            if(inputScore>=60 && inputScore<=69) gradesCount[1]++;
            if(inputScore>=50 && inputScore<=59) gradesCount[2]++;
            if(inputScore>=40 && inputScore<=49) gradesCount[3]++;
            if(inputScore>=0 && inputScore<=39) gradesCount[4]++;
            //Add one to total
            total++;
            //check best score
            if(inputScore > bestScore) bestScore = inputScore;
        }//This pattern check if its number between 0-100
        while ( in.hasNext ( "[0-9][0-9]?|100" ) );



        System.out.println ( "Negative number or not valid input. Exited." );

        System.out.println("Total number of grades :"+ total);
        System.out.println("The best score :"+ bestScore);
        System.out.println("The number of As :"+ gradesCount[0]);
        System.out.println("The number of Bs :"+ gradesCount[1]);
        System.out.println("The number of Cs :"+ gradesCount[2]);
        System.out.println("The number of Ds :"+ gradesCount[3]);
        System.out.println("The number of Fs :"+ gradesCount[4]);


    } 
}

Input/Output:

If you want to end, enter a negative number
Enter score: 5
Enter score: 10
Enter score: 15
Enter score: 20
Enter score: 33
Enter score: 99
Enter score: 100
Enter score: 44
Enter score: -3
Negative number or not valid input. Exited.
Total number of grades :8
The best score :100.0
The number of As :2
The number of Bs :0
The number of Cs :0
The number of Ds :1
The number of Fs :5

Documentation:

#1


2  

That's probably an exercise so will give just the idea. Your while loop seems to be fine, just declare two variables before it. one for holding the max grade initialized with lowest number and another for min grade with highest number.

这可能是一个练习,所以会给出这个想法。你的while循环似乎很好,只需在它之前声明两个变量。一个用于保持最低等级初始化为最低编号,另一个用于最高等级,最高编号。

score = in.nextInt(); do

score = in.nextInt();做

if ( score > highest  ) { highest = score;}
if ( score < lowest ) { lowest = score }

good luck.

#2


0  

The problem is likely that you are declaring the variable input every time the while loop runs. Instead, declare the variable like this:

问题很可能是每次while循环运行时都声明变量输入。相反,声明变量如下:

double input;

and do it before the while loop. Then in the while loop replace that line with:

并在while循环之前完成。然后在while循环中用以下代码替换该行:

input = in.nextDouble();

If this isn't the problem, it might be how you've laid out the code. The second while loop, which begins with while (score>0) should be where you have the line int score = in.nextInt() Putting this after the first while loop means that it's executed after that while loop.

如果这不是问题,那么可能就是你如何布置代码。第二个while循环以while(score> 0)开头,应该是你的行int score = in.nextInt()在第一个while循环之后放置它意味着它在while循环之后执行。

As a side note, common variables like Agrade should be written in camelCase, in which the first letter is lowercase and the letter of the next word is uppercase. This would make your variables

作为旁注,像Agrade这样的常见变量应该写在camelCase中,其中第一个字母是小写字母,下一个字母的字母是大写字母。这会使你的变量

int aGrade = 0;
int bGrade = 0;

and so on. This is just proper form and shouldn't effect your program. Also, you should probably declare score and input at the top of your program since you use them in loops. This is also good form and organization. It would look like this:

等等。这只是正确的形式,不应该影响你的程序。此外,您应该在程序顶部声明得分和输入,因为您在循环中使用它们。这也是良好的形式和组织。它看起来像这样:

int score;
double input;

#3


-1  

Check comments on code to understanding.

检查代码注释以理解。

MainClass:

import java.util.Scanner;

public class Grade
{
    public static void main ( String [ ] args )
    {
        /**
         * This gradesCount represent the A,B,C,D,E,F counts
         */
        int[] gradesCount = {0,0,0,0,0,0}; 
        double inputScore = 0;  
        double bestScore = 0;
        int total = 0;

        @SuppressWarnings ( "resource" )
        Scanner in = new Scanner(System.in);

        System.out.println ( "If you want to end, enter a negative number" );
        System.out.print("Enter score: ");

        do
        {
            inputScore = in.nextDouble ( );
            //validation for the first iteration
            if(inputScore < 0 || inputScore > 100) break;
            System.out.print("Enter score: ");
            //Add to corresponding grade count
            if(inputScore>=70 && inputScore<=100) gradesCount[0]++;
            if(inputScore>=60 && inputScore<=69) gradesCount[1]++;
            if(inputScore>=50 && inputScore<=59) gradesCount[2]++;
            if(inputScore>=40 && inputScore<=49) gradesCount[3]++;
            if(inputScore>=0 && inputScore<=39) gradesCount[4]++;
            //Add one to total
            total++;
            //check best score
            if(inputScore > bestScore) bestScore = inputScore;
        }//This pattern check if its number between 0-100
        while ( in.hasNext ( "[0-9][0-9]?|100" ) );



        System.out.println ( "Negative number or not valid input. Exited." );

        System.out.println("Total number of grades :"+ total);
        System.out.println("The best score :"+ bestScore);
        System.out.println("The number of As :"+ gradesCount[0]);
        System.out.println("The number of Bs :"+ gradesCount[1]);
        System.out.println("The number of Cs :"+ gradesCount[2]);
        System.out.println("The number of Ds :"+ gradesCount[3]);
        System.out.println("The number of Fs :"+ gradesCount[4]);


    } 
}

Input/Output:

If you want to end, enter a negative number
Enter score: 5
Enter score: 10
Enter score: 15
Enter score: 20
Enter score: 33
Enter score: 99
Enter score: 100
Enter score: 44
Enter score: -3
Negative number or not valid input. Exited.
Total number of grades :8
The best score :100.0
The number of As :2
The number of Bs :0
The number of Cs :0
The number of Ds :1
The number of Fs :5

Documentation: