Problem:
Write a program that helps visualize grade distribution in your class. To do so, your program should first ask the user the number of grades he wishes to enter. Then for every entered grade you should count the number As (90 to 100), Bs (80 to 99), Cs (70 to 89), Ds (60 to 69) and Fs (below 60) (using a switch statement). Next, you will print a bar chart of stars where for each letter the number of stars corresponds to the number of grades in that range.
Output:
Please enter the number of grades you wish to view:
8
Please enter the 8 grades:
91
92
81
82
71
72
61
50
The visual grade distribution is:
A:**
B:**
C:**
D:*
F:*
8
Please enter the 8 grades:
91
92
81
82
71
72
61
50
The visual grade distribution is:
A:**
B:**
C:**
D:*
F:*
Solution:
import java.util.Scanner; public class Problem3 { public static void main (String args []) { Scanner scan = new Scanner (System.in); int Acount = 0; int Bcount = 0; int Ccount = 0; int Dcount = 0; int Fcount = 0; System.out.println("Please enter the number of grades you wish to view:"); int numgrades = scan.nextInt(); System.out.println("Please enter the " + numgrades + " grades: "); int i = 1; while (i <= numgrades) { int grade = scan.nextInt(); grade = grade/10; switch(grade) { case 10: case 9: Acount++; break; case 8: Bcount++; break; case 7: Ccount++; break; case 6: Dcount++; break; default: Fcount++; break; } i++; } System.out.println("The visual grade distribution is:"); System.out.print("A:"); int j = 1; while(j <= Acount) { System.out.print("*"); j++; } System.out.println(); System.out.print("B:"); j = 1; while(j <= Bcount) { System.out.print("*"); j++; } System.out.println(); System.out.print("C:"); j = 1; while(j <= Ccount) { System.out.print("*"); j++; } System.out.println(); System.out.print("D:"); j = 1; while(j <= Dcount) { System.out.print("*"); j++; } System.out.println(); System.out.print("F:"); j = 1; while(j <= Fcount) { System.out.print("*"); j++; } System.out.println(); } }
No comments :
Post a Comment