如何使用java中的用户输入结束程序?

如何使用java中的用户输入结束程序?,java,Java,我目前无法用用户输入结束我的程序。例如,我现在只能在用户输入了20个数字后才能结束。我想让它做的是,如果用户输入的数字超过100,它应该停止程序,并显示一个直方图以及一个数字,以显示用户每次输入数字的次数。对不起,如果我没有任何意义,我会更新这篇文章,如果任何进一步的信息是必要的。这是我的密码 import java.util.Scanner; public class Marks { /** * @param args the command line arguments

我目前无法用用户输入结束我的程序。例如,我现在只能在用户输入了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);
    }
}
import java.util.Scanner;
公开课分数{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
int[]数组=新的int[23];
int num=0;
整数计数=0;
int-total=0;
System.out.println(“输入0到100范围内的学生分数\n”);
用于(计数=0;计数100)
{
System.out.println(“无效数字。请输入有效数字。”);
num=scan.nextInt();
}
数组[计数]=num;
总数=计数;
}
System.out.println(“0-100之间的数字出现的次数”);
字符串[]星号={“0-29 |”,“30-39 |”,“40-69 |”,“70-100 |”};//4个字符串

对于(count=0;count当用户交互时,您可以编写

System.exit(0);

当用户交互时,您可以编写

System.exit(0);
这应该能奏效不


这应该可以解决问题否?

您是否尝试过使用System.exit(0)?

您是否尝试过使用System.exit(0)?

您应该检查如何使用while循环

而不是for循环。并且有一些类似

while( userinput < 100)
{
 //insert code here
}
while(用户输入<100)
{
//在这里插入代码
}

您应该检查如何使用while循环

而不是for循环。并且有一些类似

while( userinput < 100)
{
 //insert code here
}
while(用户输入<100)
{
//在这里插入代码
}

在匹配条件下,您可以使用系统。退出(0)


在匹配条件下,您可以使用System.exit(0)


您的代码中有错误

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
这是您的更新代码

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
这是您的更新代码

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);
}
}

这修复了…又快又脏:

    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;
    }
for(计数=0;计数100)
{
如果(数值>100){
计数=20;
打破
}
System.out.println(“无效数字。请输入有效数字。”);
num=scan.nextInt();
}
数组[计数]=num;
总数=计数;
}

<>但是我不推荐这种方法。考虑使用while循环。

这个修复…快速和肮脏:

    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;
    }
for(计数=0;计数100)
{
如果(数值>100){
计数=20;
打破
}
System.out.println(“无效数字。请输入有效数字。”);
num=scan.nextInt();
}
数组[计数]=num;
总数=计数;
}

<>但是我不推荐这种方法。考虑使用while while循环。

我试着做这个,但是它不显示直方图,也不让我输入超过1个数字,但是它不显示直方图,也不让我输入超过1个数字。我应该回答。看看我的答案。它应该是工作的。它起作用了,但在我的柱状图上,当我只输入三个数字时,它显示的*太多了。它应该只显示三个数字,无论哪个范围。是的,这是问题,它不应该这样做。你输入了四个数字。数字44、44和55应该显示在40-69范围内。学生总数也应该说3而不是2,因为你这是3个与标记范围一致的数字。0-29中不应该显示任何内容,因为没有适合该范围的数字。啊!谢谢你,它起作用了。我从来没有想到我应该将20改为总数。我不知道它会起作用,也没有想到它。不仅你应该看到我是否更新了if条件.竖起大拇指,现在接受答案:)它起作用了,但在我的柱状图上,当我只输入三个数字时,它显示了太多的*。它应该只在任意范围内显示三个数字是的,这是问题,它不应该这样做。你输入了四个数字。数字44、44和55的显示范围应为40-69。学生总数也应该是3而不是2,因为你输入了3个与分数范围一致的数字。0-29中不应该显示任何内容,因为没有适合该范围的数字。啊!谢谢你,成功了。我从来没有想到我应该把20改成总数。我不知道它会起作用,也没有想到。不仅你应该看到我更新了if条件。竖起大拇指,现在接受答案:)