简体   繁体   中英

Write a program with while loop code in it to calculate the amount of exam scores enter by a user

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."

For Example, input: 98,87,86,85,85,78,73,72,72,70,66,63,50,-1

Output should be:

Total number of grades entered was 13

A's =1

B's=4

C's=5

D's=2

F's=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 :

88 98 78 68 55 45 -1

Enter exam score as integer percentage in range 0 to 100 :

Enter exam score as integer percentage in range 0 to 100 :

Enter exam score as integer percentage in range 0 to 100 :

Enter exam score as integer percentage in range 0 to 100 :

Enter exam score as integer percentage in range 0 to 100 :

Enter exam score as integer percentage in range 0 to 100 :

Total number of grades :6

Total number of A grades :1

Total number of B grades :1

Total number of C grades :1

Total number of D grades :1

Total number of F grades :2

In the output it keeps repeating "Enter exam score as integer percentage in range 0 to 100 :"

How can I write the code to only show it once?

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.

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.

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:


 //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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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