Java 为什么我的试捕卡在一个循环中?

Java 为什么我的试捕卡在一个循环中?,java,while-loop,try-catch,Java,While Loop,Try Catch,我希望用户在数组中输入整数。我编写了一个循环,其中包含一个try-and-catch,以防用户插入一个非整数。如果为真,则有一个布尔变量使循环继续。这样,用户将得到一次又一次的提示 但是,当我运行它时,它会陷入一个循环中,重复“请输入#”和“需要一个整数”,而不允许用户输入新的数字。如果捕获到异常,我会重置该数字。我不明白 import java.util.*; public class herp { //The main accesses the methods and uses th

我希望用户在数组中输入整数。我编写了一个循环,其中包含一个try-and-catch,以防用户插入一个非整数。如果为真,则有一个布尔变量使循环继续。这样,用户将得到一次又一次的提示

但是,当我运行它时,它会陷入一个循环中,重复“请输入#”和“需要一个整数”,而不允许用户输入新的数字。如果捕获到异常,我会重置该数字。我不明白

import java.util.*;
public class herp
{
    //The main accesses the methods and uses them.
    public static void main (String[] args)
    { 
        Scanner scan = new Scanner(System.in);
        System.out.println("Hello and welcome!\n");
        System.out.print("This program stores values into an array"+" and prints them.\n");
        System.out.println("Please enter how many numbers would like to store:\n");
        int arraysize = scan.nextInt();

        int[]   mainarray = new int[arraysize+1];
        int     checkint  = 0;
        boolean notint    = true;
        int     prompt    = 1;

        while (prompt < mainarray.length)
        {
            // Not into will turn true and keep the loop going if the user puts in a
            // non integer. But why is it not asking the user to put it a new checkint?
            while(notint)
            {
                try
                {
                    notint = false;
                    System.out.println("Please enter #"+ prompt); 
                    checkint = scan.nextInt();
                } 
                catch(Exception ex)
                {
                    System.out.println("An integer is required." + 
                                       "\n Input an integer please"); 
                    notint   = true;
                    checkint = 1;
                    //See, here it returns true and checkint is reset.
                }      
            }
            mainarray[prompt] = checkint;
            System.out.println("Number has been added\n");
            prompt++;
            notint = true;
        }
    } 
}
import java.util.*;
公共类herp
{
//main访问并使用这些方法。
公共静态void main(字符串[]args)
{ 
扫描仪扫描=新扫描仪(System.in);
System.out.println(“您好,欢迎!\n”);
System.out.print(“此程序将值存储到数组“+”中并打印它们。\n”);
System.out.println(“请输入要存储的数字数量:\n”);
int arraysize=scan.nextInt();
int[]mainarray=新的int[arraysize+1];
int-checkint=0;
布尔notint=true;
int-prompt=1;
while(提示符
一旦扫描仪抛出
输入不匹配异常
就无法继续使用。如果输入不可靠,请不要使用
scanner.nextInt()
使用
scanner.next()
获取
字符串
,然后将字符串转换为整数

替换:

checkint = scan.nextInt();
与:


一旦扫描仪抛出
输入不匹配异常
,就无法继续使用。如果输入不可靠,请不要使用
scanner.nextInt()
使用
scanner.next()
获取
字符串
,然后将字符串转换为整数

替换:

checkint = scan.nextInt();
与:


我已经纠正了它如下。我不依赖异常,但检查下一个扫描仪输入是否为int(使用hasNextInt())。如果不是int,只需使用Scanner令牌并等待下一个用户输入。 除了将0作为第一个数组元素插入外,它似乎还在工作,因为您是从1开始索引提示的

public class Herp {

    //The main accesses the methods and uses them.
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Hello and welcome!\n");
        System.out.print("This program stores values into an array" + " and prints them.\n");
        System.out.println("Please enter how many numbers would like to store:\n");
        int arraysize = scan.nextInt();

        int[] mainarray = new int[arraysize + 1];
        int checkint = 0;
        boolean notint = true;
        int prompt = 1;

        while (prompt < mainarray.length) {
            while (notint) {
                notint = false;
                System.out.println("Please enter #" + prompt);
                // check if int waits for us in Scanner
                if (scan.hasNextInt()) {
                    checkint = scan.nextInt();
                } else {
                    // if not, consume Scanner token
                    scan.next(); 
                    System.out.println("An integer is required."
                        + "\n Input an integer please");
                    notint = true;
                }
            }
            mainarray[prompt] = checkint;
            System.out.println("Number has been added\n");
            prompt++;
            notint = true;
        }

        for (int i : mainarray) {
            System.out.print(i + " ");
        }
    }
}
公共类Herp{
//main访问并使用这些方法。
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“您好,欢迎!\n”);
System.out.print(“此程序将值存储到数组“+”中并打印它们。\n”);
System.out.println(“请输入要存储的数字数量:\n”);
int arraysize=scan.nextInt();
int[]mainarray=新的int[arraysize+1];
int-checkint=0;
布尔notint=true;
int-prompt=1;
while(提示符
我已经纠正了它,如下所示。我不依赖异常,但检查下一个扫描仪输入是否为int(使用hasNextInt())。如果不是int,只需使用Scanner令牌并等待下一个用户输入。 除了将0作为第一个数组元素插入外,它似乎还在工作,因为您是从1开始索引提示的

public class Herp {

    //The main accesses the methods and uses them.
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Hello and welcome!\n");
        System.out.print("This program stores values into an array" + " and prints them.\n");
        System.out.println("Please enter how many numbers would like to store:\n");
        int arraysize = scan.nextInt();

        int[] mainarray = new int[arraysize + 1];
        int checkint = 0;
        boolean notint = true;
        int prompt = 1;

        while (prompt < mainarray.length) {
            while (notint) {
                notint = false;
                System.out.println("Please enter #" + prompt);
                // check if int waits for us in Scanner
                if (scan.hasNextInt()) {
                    checkint = scan.nextInt();
                } else {
                    // if not, consume Scanner token
                    scan.next(); 
                    System.out.println("An integer is required."
                        + "\n Input an integer please");
                    notint = true;
                }
            }
            mainarray[prompt] = checkint;
            System.out.println("Number has been added\n");
            prompt++;
            notint = true;
        }

        for (int i : mainarray) {
            System.out.print(i + " ");
        }
    }
}
公共类Herp{
//main访问并使用这些方法。
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“您好,欢迎!\n”);
System.out.print(“此程序将值存储到数组“+”中并打印它们。\n”);
System.out.println(“请输入要存储的数字数量:\n”);
int arraysize=scan.nextInt();
int[]mainarray=新的int[arraysize+1];
int-checkint=0;
布尔notint=true;
int-prompt=1;
while(提示符