编写一个带有while循环代码的程序来计算用户输入的考试分数

时间:2015-04-15 11:21:17

标签: java if-statement while-loop do-while

我在我正在做的任务中为以下问题编写了一个程序。

“编写一个程序,以0到100的整数百分比的形式读取考试分数列表。显示每个字母等级类别的成绩总数和成绩数量如下:90- 100是A,80-89是B,70-79是C,60-69是D,0-59是F.使用负分数作为标记值来表示输入的结束。“ < / p>

例如,输入: 98,87,86,85,85,78,73,72,72,70,66,63,50,-1

输出应为:

输入的成绩总数为13

A = 1

B的= 4

C'S = 5

D'S = 2

F公司= 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

以下是该计划的输出。

将考试分数输入为0到100范围内的整数百分比:

88 98 78 68 55 45 -1

将考试分数输入为0到100范围内的整数百分比:

将考试分数输入为0到100范围内的整数百分比:

将考试分数输入为0到100范围内的整数百分比:

将考试分数输入为0到100范围内的整数百分比:

将考试分数输入为0到100范围内的整数百分比:

将考试分数输入为0到100范围内的整数百分比:

总成绩:6

A等级总数:1

B等级总数:1

C等级总数:1

D等级总数:1

F等级总数:2

在输出中,它会不断重复“在0到100范围内输入检查分数作为整数百分比:”

如何编写代码只显示一次?

3 个答案:

答案 0 :(得分:2)

如果您不想多次看到此消息,请从循环中删除System.out.println("Enter exam score as integer percentage in range 0 to 100 :");

答案 1 :(得分:0)

连续打印出来的原因是因为您正在使用的“扫描仪”类没有暂停输入。

我建议您使用BufferedReader。

我假设您需要让用户每次都输入一个值。因此,如果您删除文本以解决多重显示问题,则不会向用户提供消息。

我附上了一段代码,其中包含一个可以解决问题的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


希望这可以帮助您并回答您的问题:)

让我知道结果

答案 2 :(得分:0)

感谢您的所有答案。

这是最终的代码,它完美无缺。

 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