我目前无法通过用户输入结束我的程序。我现在只能在用户输入20个数字时结束。我想要它做的是,如果用户输入的数字超过100,它应该停止程序并显示直方图和数字,以显示用户每次输入数字的次数。对不起,如果我没有任何意义,如果需要进一步的信息,我会更新这篇文章。这是我的代码。
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);
}
}
答案 0 :(得分:3)
当用户进行交互时,您可以编写
System.exit(0);
答案 1 :(得分:1)
您的代码中存在错误
这是您更新的代码
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);
}
}
这是输出
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
答案 2 :(得分:0)
else if (num >100) System.exit(0);
这应该不行吗?
答案 3 :(得分:0)
您是否尝试过使用System.exit(0)?
答案 4 :(得分:0)
你应该检查如何使用while循环。
而不是for循环。并有一些像
的声明while( userinput < 100)
{
//insert code here
}
答案 5 :(得分:0)
在匹配条件下,您可以使用System.exit(0)
if(matchCondition) {
System.exit(0);
}
答案 6 :(得分:0)
这个修复......快速而肮脏:
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;
}
但我不推荐这种方法。请考虑使用while循环。