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 class NumberGrades 
{
    public static void main(String [] args)
    {

        Scanner keyboard= new Scanner(System.in);
        Console c = System.console();
        int score=0, n1;
        String grade;
        System.out.print("Please enter your score: ");

         try { 
        score= Integer.valueOf(c.readLine());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            }



       if(score>=90)
       {

         grade="A";
         System.out.println("grade: " + grade);

       }

       else
       if(score>=80)
       {
         System.out.print("Please enter your score: ");
        grade= "B";
          System.out.println("grade: " + grade);
       }
       else
       if(score>=70)
       {

        grade = "C";
          System.out.println("grade: " + grade);
       }
       else
       if(score>=60)
       {
        grade= "D";
          System.out.println("grade: " + grade);

       }
       else 
       if(score<=59)
       {
        grade ="F";
          System.out.println("grade: " + grade);
       }
       else
       grade= "X";

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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) {
  input = c.readLine();
  gradecounter++;
  if (input>=90){
    aGrade++;
  }
  //etc.
}
System.out.println("Total grades: "+gradecounter+
                   "A Grades: "+aGrades+"which is "+aGrade/gradecounter;

Etc. Etc. Etc.

Edited 5 Years Ago by Slyvr: n/a

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;


public class AverageGrade {

	public AverageGrade() {
		System.out.println("Exam Statistics");
		System.out.println();
		displayMenu();
	}
	
	private void displayMenu() {
		int[] gradeBuckets = new int[5]; // 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 9: gradeBuckets[0]++; break;
					case 8: gradeBuckets[1]++; break;
					case 7: gradeBuckets[2]++; break;
					case 6: gradeBuckets[3]++; break;
					case 5:
					case 4:
					case 3:
					case 2:
					case 1:
					case 0:	gradeBuckets[4]++; break;
				}
			}			
		} while (input >= 0);
		
		displayStatistics(gradeBuckets);
	}
	
	private void displayStatistics(int[] gradeBuckets) {
		int sum = 0;
		for (int grade : gradeBuckets)
			sum += grade;
		
		System.out.println();
		System.out.println("Total number of grades = " + sum);
		System.out.println("Number of A's = " + gradeBuckets[0] +
				" which is " + getAverage(gradeBuckets[0], sum) + "%");
		System.out.println("Number of B's = " + gradeBuckets[1] +
				" which is " + getAverage(gradeBuckets[1], sum) + "%");
		System.out.println("Number of C's = " + gradeBuckets[2] +
				" which is " + getAverage(gradeBuckets[2], sum) + "%");
		System.out.println("Number of D's = " + gradeBuckets[3] +
				" which is " + getAverage(gradeBuckets[3], sum) + "%");
		System.out.println("Number of F's = " + gradeBuckets[4] +
				" which is " + getAverage(gradeBuckets[4], sum) + "%");
	}
	
	private String getAverage(int value, int sum) {
		return new DecimalFormat("#.#").format(100.0 * value / sum);
	}
	
	public static void main(String[] args) {
		new AverageGrade();
	}
}

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;


public class AverageGrade {

	public AverageGrade() {
		System.out.println("Exam Statistics");
		System.out.println();
		displayMenu();
	}
	
	private void displayMenu() {
		int[] gradeBuckets = new int[5]; // 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 9: gradeBuckets[0]++; break;
					case 8: gradeBuckets[1]++; break;
					case 7: gradeBuckets[2]++; break;
					case 6: gradeBuckets[3]++; break;
					case 5:
					case 4:
					case 3:
					case 2:
					case 1:
					case 0:	gradeBuckets[4]++; break;
				}
			}			
		} while (input >= 0);
		
		displayStatistics(gradeBuckets);
	}
	
	private void displayStatistics(int[] gradeBuckets) {
		int sum = 0;
		for (int grade : gradeBuckets)
			sum += grade;
		
		System.out.println();
		System.out.println("Total number of grades = " + sum);
		System.out.println("Number of A's = " + gradeBuckets[0] +
				" which is " + getAverage(gradeBuckets[0], sum) + "%");
		System.out.println("Number of B's = " + gradeBuckets[1] +
				" which is " + getAverage(gradeBuckets[1], sum) + "%");
		System.out.println("Number of C's = " + gradeBuckets[2] +
				" which is " + getAverage(gradeBuckets[2], sum) + "%");
		System.out.println("Number of D's = " + gradeBuckets[3] +
				" which is " + getAverage(gradeBuckets[3], sum) + "%");
		System.out.println("Number of F's = " + gradeBuckets[4] +
				" which is " + getAverage(gradeBuckets[4], sum) + "%");
	}
	
	private String getAverage(int value, int sum) {
		return new DecimalFormat("#.#").format(100.0 * value / sum);
	}
	
	public static void main(String[] args) {
		new AverageGrade();
	}
}

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

This article has been dead for over six months. Start a new discussion instead.