Reputation: 83
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);
}
}
Upvotes: 2
Views: 10017
Reputation: 548
This fixes... Quick and dirty:
for (count = 0; count <= 20; count++)
{
System.out.println ("Enter a number:");
num = scan.nextInt();
while (num < 0 || num > 100)
{
if (num > 100) {
count = 20;
break;
}
System.out.println ("Invalid number. Enter a valid number.");
num = scan.nextInt();
}
array[count] = num;
total=count;
}
But I wouldn't recommend this approach. Consider using while loop instead.
Upvotes: 0
Reputation: 17461
You have errors in your code
Here is your updated code
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 = 1;
System.out.println ("Enter students marks in the range 0 to 100\n");
loop: for (count = 0; count <= 20; count++) {
System.out.println("Enter a number:");
num = scan.nextInt();
if (num < 0 || num > 100) {
break loop;
}
array[count] = num;
total = count+1;
}
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 = 1; count <= total; count++)
{
num=array[count];
if (num >=0 && num<=29) asterisk [0] +="*";
else if (num>29 && num<=39) asterisk[1] +="*";
else if (num>39 && num <=69) asterisk[2] +="*";
else if (num >69 && 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);
}
}
Here is the output
Enter students marks in the range 0 to 100
Enter a number:
1
Enter a number:
10
Enter a number:
50
Enter a number:
111
How many times a number between 0-100 occur.
0- 29 | **
30- 39 |
40- 69 | *
70- 100 |
The total amount of students is 3
Upvotes: 1
Reputation: 2895
On the matching condition u can use System.exit(0)
if(matchCondition) {
System.exit(0);
}
Upvotes: 0
Reputation: 603
You should checkout how to use while loops.
Instead of the for loop. and have some statment like
while( userinput < 100)
{
//insert code here
}
Upvotes: 0