I am currently having trouble to end my program with a users input. I currently now can only end when the user has inputted 20 numbers for example. What I want it to do is if a user inputs a number over 100, it should stop the program and display a histogram as well as a number to show the amount of times the user has inputted a number every time. Sorry if I am not making any sense, I will update this post if any further information is needed. Here is my code.
import java.util.Scanner;
public class Marks {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] array = new int[23];
int num = 0;
int count = 0;
int total = 0;
System.out.println ("Enter students marks in the range 0 to 100\n");
for (count = 0; count <= 20; count++)
{
System.out.println ("Enter a number:");
num = scan.nextInt();
while (num < 0 || num > 100)
{
System.out.println ("Invalid number. Enter a valid number.");
num = scan.nextInt();
}
array[count] = num;
total=count;
}
System.out.println ("How many times a number between 0-100 occur.");
String[] asterisk = {"0- 29 | ", "30- 39 | ","40- 69 | ", "70- 100 | "}; //4 strings
for (count = 0; count <= 20; count++)
{
num=array[count];
if (num <=29) asterisk [0] +="*";
else if (num <=39) asterisk[1] +="*";
else if (num <=69) asterisk[2] +="*";
else if (num <=100) asterisk[3] +="*";
}
for (count =0;count < 4;count++)
System.out.println(asterisk[count]);
System.out.println("The total amount of students is " + total);
}
}