java 编写一个包含 while 循环代码的程序来计算用户输入的考试分数数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29648866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write a program with while loop code in it to calculate the amount of exam scores enter by a user
提问by Evert Van Eeden
I wrote a program for the following question in a task I'm doing.
我在我正在做的任务中为以下问题编写了一个程序。
"Write a program to read a list of exam scores given as integer percentages in the range 0 to 100. Display the total number of grades and the number of grades in each letter-grade category as follows: 90-100 is A, 80-89 is B, 70-79 is C, 60-69 is D, and 0-59 is F. Use negative score as a sentinel value to indicate the end of the input."
“编写一个程序来读取以 0 到 100 范围内的整数百分比给出的考试分数列表。显示总成绩数和每个字母等级类别的成绩数如下:90-100 是 A,80- 89 是 B,70-79 是 C,60-69 是 D,0-59 是 F。使用负分数作为哨兵值来表示输入的结束。”
For Example, input: 98,87,86,85,85,78,73,72,72,70,66,63,50,-1
例如输入:98,87,86,85,85,78,73,72,72,70,66,63,50,-1
Output should be:
输出应该是:
Total number of grades entered was 13
输入的成绩总数为 13
A's =1
A = 1
B's=4
B's=4
C's=5
Cs=5
D's=2
Ds=2
F's=1
Fs=1
//This is the code I wrote.//
import java.util.Scanner;
public class grade
{
public static void main(String[] args)
{
int A_grades = 0;
int B_grades = 0;
int C_grades = 0;
int D_grades = 0;
int F_grades = 0;
int count=0;
Scanner in = new Scanner(System.in);
System.out.println("Enter exam score as integer percentage in range 0 to 100 :");
int score = in.nextInt();
while(score>0)
{
count++;
if(score>=90 && score<=100)
A_grades++;
else if(score>=80 && score<=89)
B_grades++;
else if(score>=70 && score<=79)
C_grades++;
else if(score>=60 && score<=69)
D_grades++;
else if(score>=0 && score<=59)
F_grades++;
System.out.println("Enter exam score as integer percentage in range 0 to 100 :");
score = in.nextInt();
} //end while
System.out.println("Total number of grades :"+ count);
System.out.println("Total number of A grades :"+ A_grades);
System.out.println("Total number of B grades :"+ B_grades);
System.out.println("Total number of C grades :"+ C_grades);
System.out.println("Total number of D grades :"+ D_grades);
System.out.println("Total number of F grades :"+ F_grades);
} // end main
} // end class
Below is the Output of the program.
下面是程序的输出。
Enter exam score as integer percentage in range 0 to 100 :
以 0 到 100 范围内的整数百分比形式输入考试分数:
88 98 78 68 55 45 -1
88 98 78 68 55 45 -1
Enter exam score as integer percentage in range 0 to 100 :
以 0 到 100 范围内的整数百分比形式输入考试分数:
Enter exam score as integer percentage in range 0 to 100 :
以 0 到 100 范围内的整数百分比形式输入考试分数:
Enter exam score as integer percentage in range 0 to 100 :
以 0 到 100 范围内的整数百分比形式输入考试分数:
Enter exam score as integer percentage in range 0 to 100 :
以 0 到 100 范围内的整数百分比形式输入考试分数:
Enter exam score as integer percentage in range 0 to 100 :
以 0 到 100 范围内的整数百分比形式输入考试分数:
Enter exam score as integer percentage in range 0 to 100 :
以 0 到 100 范围内的整数百分比形式输入考试分数:
Total number of grades :6
总年级数:6
Total number of A grades :1
A级总数:1
Total number of B grades :1
B级总数:1
Total number of C grades :1
C级总数:1
Total number of D grades :1
D级总数:1
Total number of F grades :2
F级总数:2
In the output it keeps repeating "Enter exam score as integer percentage in range 0 to 100 :"
在输出中,它不断重复“以 0 到 100 范围内的整数百分比形式输入考试分数:”
How can I write the code to only show it once?
如何编写代码只显示一次?
回答by Eran
Remove System.out.println("Enter exam score as integer percentage in range 0 to 100 :");
from your loop if you don't want to see this message multiple times.
System.out.println("Enter exam score as integer percentage in range 0 to 100 :");
如果您不想多次看到此消息,请从循环中删除。
回答by TejjD
the reason it is continuously printing that out is because the "Scanner" class that you are using does not pause for input.
它不断打印出来的原因是因为您使用的“扫描仪”类不会暂停输入。
I would suggest that you use a BufferedReader.
我建议您使用 BufferedReader。
I assume that you need to ask the user to enter a value each time. So if you remove the text to solve the multiple display issue, the user wont be given the message.
我假设您每次都需要要求用户输入一个值。因此,如果您删除文本以解决多重显示问题,则不会向用户提供消息。
I have attached a snippet of the code, with a BufferedReader that will solve your problem:
我附上了一段代码,带有 BufferedReader 可以解决您的问题:
//This is the code I wrote.//
import java.util.Scanner;
import java.io.*;
import java.io.BufferedReader;
import java.util.*;
public class Grade{
public static void main(String[] args){
int A_grades = 0;
int B_grades = 0;
int C_grades = 0;
int D_grades = 0;
int F_grades = 0;
int count=0;
try{
//Scanner in = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in) );
System.out.println("Enter exam score as integer percentage in range 0 to 100 :");
int score = Integer.parseInt(br.readLine());
while(score>0){
count++;
if(score>=90 && score<=100)
A_grades++;
else if(score>=80 && score<=89)
B_grades++;
else if(score>=70 && score<=79)
C_grades++;
else if(score>=60 && score<=69)
D_grades++;
else if(score>=0 && score<=59)
F_grades++;
System.out.println("Enter exam score as integer percentage in range 0 to 100 :");
score = Integer.parseInt(br.readLine());
} //end while
}catch(IOException ioe){}
System.out.println("Total number of grades :"+ count);
System.out.println("Total number of A grades :"+ A_grades);
System.out.println("Total number of B grades :"+ B_grades);
System.out.println("Total number of C grades :"+ C_grades);
System.out.println("Total number of D grades :"+ D_grades);
System.out.println("Total number of F grades :"+ F_grades);
} // end main
} // end class
Hope this helps you and answers your question :)
希望这可以帮助您并回答您的问题:)
Let me know of the outcome
让我知道结果
回答by Evert Van Eeden
Thank you for all the answers.
谢谢大家的回答。
This is the final code and it works perfectly.
这是最终的代码,它完美地工作。
import java.util.Scanner;
public class grade
{
public static void main(String[] args)
{
int A_grades = 0;
int B_grades = 0;
int C_grades = 0;
int D_grades = 0;
int F_grades = 0;
int count=0;
Scanner in = new Scanner(System.in);
System.out.println("Enter exam score as integer percentage in range 0 to 100");
System.out.println("And enter a negative score at the end of the list :");
int score = in.nextInt();
while(score>0)
{
count++;
if(score>=90 && score<=100)
A_grades++;
else if(score>=80 && score<=89)
B_grades++;
else if(score>=70 && score<=79)
C_grades++;
else if(score>=60 && score<=69)
D_grades++;
else if(score>=0 && score<=59)
F_grades++;
score = in.nextInt();
} //end while
System.out.println("Total number of grades :"+ count);
System.out.println("Total number of A grades :"+ A_grades);
System.out.println("Total number of B grades :"+ B_grades);
System.out.println("Total number of C grades :"+ C_grades);
System.out.println("Total number of D grades :"+ D_grades);
System.out.println("Total number of F grades :"+ F_grades);
} // end main
} // end class