Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有try-catch的Java-while循环导致程序看起来什么都不做_Java - Fatal编程技术网

带有try-catch的Java-while循环导致程序看起来什么都不做

带有try-catch的Java-while循环导致程序看起来什么都不做,java,Java,当我运行下面的代码时,它会清除控制台,但似乎没有执行任何操作 int x = 0; //exception catching loop while (x == 0); ArrayList<Integer> values = new ArrayList<Integer>(); Scanner Scores = new Scanner(System.in);//creates a scanner for the golfer'

当我运行下面的代码时,它会清除控制台,但似乎没有执行任何操作

int x = 0; //exception catching loop
while (x == 0);

            ArrayList<Integer> values = new ArrayList<Integer>();
            Scanner Scores = new Scanner(System.in);//creates a scanner for the golfer's last 5 scores
            System.out.println("Please enter the scores from your last five rounds of eighteen-hole golf, in order."); 
            try
            { 
            for(int i = 0; i < 5; i++)
                {
                System.out.println("Please enter one score.");
                int scorecard = Scores.nextInt();
                values.add(scorecard); 
                }

        }catch(InputMismatchException ex){

            System.out.println("There has been an error. Please enter your last five scores again."); 
            x = 1; //if successful, will allow for continuation of the program. 
        }
然而,当我执行while循环并尝试catch行时,程序运行良好,我只是不确定如何正确执行此段。它没有显示错误,但无法运行。困扰我的是,我似乎无法正确编写异常捕获系统的代码。我是Java的新手

while (x == 0);
这将无限次地运行空语句,因为循环中的x永远不会改变。除非您希望此处有一个无限循环,否则请删除分号。

您的while循环:

while (x == 0);
是一个永不结束的不定式循环。如果您想让它做任何合理的事情,您应该使用while之后删除冒号,并在括号{}中包含while循环下面的所有代码