Issue
I'm adding several numbers that were entered by a user and adding them to array list.
My code so far:
package project143;
import java.util.*;
/**
* @author --
*/
public class Histogram {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Input for grades
int mark = 0;
List<Integer> list = new ArrayList<Integer>();
while (mark >= 0 && mark <= 100) {
System.out.println("Enter students mark:");
mark = input.nextInt();
if (mark >= 0 && mark <= 100) {
list.add(mark);
}
}
System.out.println(list);
}
}
Now, I need to count how many numbers from the list
are within following ranges (0-29 , 30-39 , 40-69 , 70-100)
Once I know how many numbers there are within each range, I need to display "" next to each range, so for example there are 10 numbers within range of 0 - 29, therefore I need to display 10 stars (***).
How can I achieve this?
Solution
You can do it this way without a list by putting this code in the loop you get input from...
if (mark <= 29) ++bombout;
else if (mark <= 39) ++fail;
else if (mark <= 69) ++pass;
else ++excellent;
...or if you want to use a List, iterate over it and put the code above in the loop.
Answered By - xagyg
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)