I'm trying to make a program that asks the user how many grades they want to enter. Then, after they enter the grades a horizontal bar chart is printed of how many of the grades fall between a certain range using asterisks(range is 0-9, 10-19, 20-29, etc. to 100). Right now, my code takes the user input, stores the entered grades and prints the values that fall between the specified range but does so incorrectly (i.e. if two grades fall between 80-89 it will print 80-89:* then 80-89:* below it as oppose to 80-89:**). Lastly, I can't figure out an easier way to do this instead of printing multiple if statements. I appreciate everyone's help!
public void grades(){
Scanner in = new Scanner(System.in);
System.out.println("How many grades would you like to enter? "); //user input how many grades user would like to enter
int q = in.nextInt();
double[] grades = new double[q]; //initialized array
double sum = 0;
for (int counter = 0; counter < q; counter++){ //user enters # of grades they requested to enter
System.out.println("Enter your grades: ");
double grade = in.nextInt();
grades[counter] = grade; //grade values stored in array
}
System.out.println("Bar chart of grades: "); //title of printed list
for (int i = 0; i < q; i++){ //loop scans grades. Iterate through the grades array with filled values
if (grades[i] <= 9) { //if grades stored in array fall within range
System.out.println("0-9: " + '*'); //print those grades on graph
}
}
}
Aucun commentaire:
Enregistrer un commentaire