I am working on a basic program (console).
The program should allow the user to enter in various marks, until the mark entered exceeds 100. At this point the program should display a histogram. Each star represents a student who achieved a module mark in the range shown.
This is an example of the output.
0 - 29 xxx
30 - 39 xxxxx
40 - 69 xxxxxxx
70 - 100 xxxx
20 students in total.
As the user enters each mark, there ought to be a counter that increases in value and print the total number of marks entered.
I want to make sure that the program is as efficient as possible but also understandable
Code
Working with @Dici on collabedit I have an amazing answer to my question:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] ranges = { 0,29,39,69,100 };
int[] inRange = new int[ranges.length - 1];
int mark;
do {
System.out.println("Enter Mark:");
mark = sc.nextInt();
for (int j=1 ; j<ranges.length ; j++)
if (ranges[j-1] <= mark && mark <= ranges[j]) {
inRange[j-1]++;
break;
}
} while (mark <= 100);
System.out.println(Arrays.toString(inRange));
String s = "The number of students that have scored between %d and %d is: ";
int k = 0;
for (int i=0 ; i<ranges.length - 1 ; i++) {
System.out.print(String.format(s,ranges[i] + k,ranges[i + 1]));
for (int r = 0; r<inRange[i] ; r++)
System.out.print("*");
System.out.println();
k = 1;
}
sc.close();
Thank you again for your amazing help!