write a program to read in a list of exam scores (integer percentage in the range 0 TO 100)
and to output the total number of grades as well as the number of grades in each letter- grade category (90-100=A, 80-89=B, 70-79=C, 60-69=D and 0-59=F) and what percentage of the total grades each letter grade represents. The end of the input is indicated by a negative score as a sentinel value.(the negative value is issued only to end input from the user, so do not use it in the calculation. For example if the input is

``````98
87                 the output could look something like this:
87
86               Total number of grades = 14
85               Number of A's =1   which is 7.1%
85               Number of B's =4   which is 28.6%
78               Number of C's =6   which is 42.9%
73               Number of D's =2   which is 14.3%
72               Number of F's =1   which is 7.1%
72
70
66
63
50
-1
``````

I NEED HELP TO UNDERSTAND MORE HOW TO DO THIS PROGRAM COZ I TRIED DOING IT BUT ONLY ONE OUTPUT IS GOING OUT.

``````import java.util.*;
import java.io.Console;
import java.util.Scanner;
import javax.swing.*;
{
public static void main(String [] args)
{

Scanner keyboard= new Scanner(System.in);
Console c = System.console();
int score=0, n1;

try {
} catch (NumberFormatException e) {
e.printStackTrace();
}

if(score>=90)
{

}

else
if(score>=80)
{
}
else
if(score>=70)
{

}
else
if(score>=60)
{

}
else
if(score<=59)
{
}
else
``````

Make a variable to count the number of grades.
You'll need a loop like this:

``````int gradecounter=0, aGrade, bGrade, cGrade, etc;

while (input != null) {
if (input>=90){
}
//etc.
}

Etc. Etc. Etc.

## All 3 Replies

Make a variable to count the number of grades.
You'll need a loop like this:

``````int gradecounter=0, aGrade, bGrade, cGrade, etc;

while (input != null) {
if (input>=90){
}
//etc.
}

Etc. Etc. Etc. Instead of mashing all your code into the main method, you should try to break apart the problem into parts. So in this case, you have 2 pieces to the problem: get input until -1, display statistics.

So, something like this:

``````import java.text.DecimalFormat;
import java.util.Scanner;

System.out.println("Exam Statistics");
System.out.println();
}

int[] gradeBuckets = new int; // 0 = A, 1 = B, 2 = C, 3 = D, 4 = F
int input;

do {
System.out.print("Exam Score: ");
if ((input = new Scanner(System.in).nextInt()) >= 0) {
switch (input / 10) {
case 10:
case 5:
case 4:
case 3:
case 2:
case 1:
}
}
} while (input >= 0);

}

int sum = 0;

System.out.println();
System.out.println("Total number of grades = " + sum);
System.out.println("Number of A's = " + gradeBuckets +
" which is " + getAverage(gradeBuckets, sum) + "%");
System.out.println("Number of B's = " + gradeBuckets +
" which is " + getAverage(gradeBuckets, sum) + "%");
System.out.println("Number of C's = " + gradeBuckets +
" which is " + getAverage(gradeBuckets, sum) + "%");
System.out.println("Number of D's = " + gradeBuckets +
" which is " + getAverage(gradeBuckets, sum) + "%");
System.out.println("Number of F's = " + gradeBuckets +
" which is " + getAverage(gradeBuckets, sum) + "%");
}

private String getAverage(int value, int sum) {
return new DecimalFormat("#.#").format(100.0 * value / sum);
}

public static void main(String[] args) {
}
}``````

Console:

``````Exam Statistics

Exam Score: 98
Exam Score: 87
Exam Score: 87
Exam Score: 86
Exam Score: 85
Exam Score: 85
Exam Score: 78
Exam Score: 73
Exam Score: 72
Exam Score: 72
Exam Score: 70
Exam Score: 66
Exam Score: 63
Exam Score: 50
Exam Score: -1

Total number of grades = 14
Number of A's = 1 which is 7.1%
Number of B's = 5 which is 35.7%
Number of C's = 5 which is 35.7%
Number of D's = 2 which is 14.3%
Number of F's = 1 which is 7.1%``````

Note that instead of repeating the formatting on the statistics where the average is displayed, I used a method to do so. This allows me to change the format later on with ease. If you ever find yourself repeating the same code over and over, you probably need to break it out into a separate method like this.

Instead of mashing all your code into the main method, you should try to break apart the problem into parts. So in this case, you have 2 pieces to the problem: get input until -1, display statistics.

So, something like this:

``````import java.text.DecimalFormat;
import java.util.Scanner;

System.out.println("Exam Statistics");
System.out.println();
}

int[] gradeBuckets = new int; // 0 = A, 1 = B, 2 = C, 3 = D, 4 = F
int input;

do {
System.out.print("Exam Score: ");
if ((input = new Scanner(System.in).nextInt()) >= 0) {
switch (input / 10) {
case 10:
case 5:
case 4:
case 3:
case 2:
case 1:
}
}
} while (input >= 0);

}

int sum = 0;

System.out.println();
System.out.println("Total number of grades = " + sum);
System.out.println("Number of A's = " + gradeBuckets +
" which is " + getAverage(gradeBuckets, sum) + "%");
System.out.println("Number of B's = " + gradeBuckets +
" which is " + getAverage(gradeBuckets, sum) + "%");
System.out.println("Number of C's = " + gradeBuckets +
" which is " + getAverage(gradeBuckets, sum) + "%");
System.out.println("Number of D's = " + gradeBuckets +
" which is " + getAverage(gradeBuckets, sum) + "%");
System.out.println("Number of F's = " + gradeBuckets +
" which is " + getAverage(gradeBuckets, sum) + "%");
}

private String getAverage(int value, int sum) {
return new DecimalFormat("#.#").format(100.0 * value / sum);
}

public static void main(String[] args) {
}
}``````

Console:

``````Exam Statistics

Exam Score: 98
Exam Score: 87
Exam Score: 87
Exam Score: 86
Exam Score: 85
Exam Score: 85
Exam Score: 78
Exam Score: 73
Exam Score: 72
Exam Score: 72
Exam Score: 70
Exam Score: 66
Exam Score: 63
Exam Score: 50
Exam Score: -1

Total number of grades = 14
Number of A's = 1 which is 7.1%
Number of B's = 5 which is 35.7%
Number of C's = 5 which is 35.7%
Number of D's = 2 which is 14.3%
Number of F's = 1 which is 7.1%``````

Note that instead of repeating the formatting on the statistics where the average is displayed, I used a method to do so. This allows me to change the format later on with ease. If you ever find yourself repeating the same code over and over, you probably need to break it out into a separate method like this.

@ztini thanks that helps a lot im not good at programing it already takes me 2days to figure out,thank u so much..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.