Java 如何在while循环中使用Scanner hasnetint()?

Java 如何在while循环中使用Scanner hasnetint()?,java,while-loop,java.util.scanner,Java,While Loop,Java.util.scanner,我无法摆脱while循环 我不知道为什么sc.hasNextInt()在上次读取数字后不返回false 我应该使用另一种方法还是代码中有错误 public static void main(String[] args) { // Creating an array by user keyboard input Scanner sc = new Scanner(System.in); System.out.println("Length of a

我无法摆脱while循环

我不知道为什么
sc.hasNextInt()
在上次读取数字后不返回false

我应该使用另一种方法还是代码中有错误

public static void main(String[] args) {

        // Creating an array by user keyboard input
        Scanner sc = new Scanner(System.in);

        System.out.println("Length of array: ");
        int[] numbers = new int[sc.nextInt()];

        System.out.printf("Type in integer elements of array ", numbers.length);
        int index = 0;
        **while ( sc.hasNextInt()) {**
            numbers[index++] = sc.nextInt();


        }
        // created method for printing arrays
        printArray(numbers);
        sc.close();


}

当您从控制台接收输入时,放置在
中的Scanner
hasnetint()
方法,而
循环条件将继续读取(意味着循环将继续),直到发生以下情况之一:

  • 提交非数字符号(例如字母)
  • 您提交一个所谓的“”字符,这是一个特殊符号,告诉扫描仪停止读取
因此,在您的情况下,while循环条件中不能有
hasnetint()
——我在下面展示了一个解决方案,其中包含一个可以使用的计数器变量

然而
while
循环中的
hasnetint()方法
在从控制台以外的源读取时有其实际用途,例如从字符串或文件读取。受示例启发,假设我们有:

String s = "Hello World! 3 + 3.0 = 6 ";
然后,我们可以将字符串
s
作为输入源传递给扫描仪(注意,我们没有将
System.in
传递给构造函数):

然后循环直到
hasNext()
,检查输入中是否有任何类型的其他标记。在循环内部,使用
hasNextInt()
检查此令牌是否为int并打印它,否则使用
next()
将令牌传递给下一个令牌:

结果:

Found int value: 3
Found int value: 6
在上面的示例中,我们不能在while循环条件本身中使用
hasNextInt()
,因为该方法在找到的第一个非int字符上返回false(因此循环立即关闭,因为字符串以字母开头)

但是,我们可以使用
while(hasNextInt())
来读取

现在,问题的解决方案是将
索引
变量置于while循环条件中:

while (index < numbers.length) {
    numbers[index++] = sc.nextInt();
}
while(索引
或者为了清楚起见,制作一个特定的计数器变量:

int index = 0;
int counter = 0;
while (counter < numbers.length) {
       numbers[index++] = sc.nextInt();
       counter++;
}
int索引=0;
int计数器=0;
while(计数器
执行以下操作:

使用输入长度作为循环的结束

        // Creating an array by user keyboard input
        Scanner sc = new Scanner(System.in);

        System.out.println("Length of array: ");

        int len = sc.nextInt();
        int[] numbers = new int[len]; // use len here

        System.out.printf("Type in integer elements of array ", numbers.length);
        int index = 0;
        for (index = 0; index < len; index++) { // and use len here
            numbers[index] = sc.nextInt();
        }
        // created method for printing arrays
        printArray(numbers);
        sc.close();
//通过用户键盘输入创建数组
扫描仪sc=新的扫描仪(System.in);
System.out.println(“数组长度:”);
int len=sc.nextInt();
int[]数字=新的int[len];//在这里使用len
System.out.printf(“键入数组的整数元素”,number.length);
int指数=0;
对于(index=0;index

并且不要关闭扫描仪。

解决代码中的以下问题:

  • 将数组的大小存储到一个变量(例如
    int n=sc.nextInt();
    )中,并使用此值创建所需大小的数组(即
    int[]numbers=new int[n];
    ),还可以控制在(索引
  • )期间执行循环的次数(即
  • 尽管对于此特定程序不是必需的,但您不应在
  • 中关闭
    系统的
    扫描仪
    对象,因为它也将在
    中关闭
    系统,这将不允许使用
    系统在
    中进行任何进一步的输入(例如键盘)。这可能是一个大程序中的问题,您可能需要进一步的输入 以下是包含上述各点的代码:

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            // Creating an array by user keyboard input
            Scanner sc = new Scanner(System.in);
    
            System.out.print("Length of array: ");
            int n = sc.nextInt();
            int[] numbers = new int[n];
    
            System.out.printf("Type in %d integer elements of array: ", numbers.length);
            int index = 0;
            while (index < n && sc.hasNextInt()) {
                numbers[index++] = sc.nextInt();
            }
            // created method for printing arrays
            printArray(numbers);
        }
    
        static void printArray(int[] numbers) {
            for (int n : numbers) {
                System.out.print(n + " ");
            }
        }
    }
    

    它怎么知道最后一个号码是什么时候?它是从键盘上读取的,它无法读懂你的心思来知道何时停止询问。你应该使用输入数组长度作为循环的终止符。查看我的答案。在hasNextInt()返回false后,它应该停止询问。但我不明白为什么它不会?只有输入非整数(例如字母)它才会停止。@WJS感谢您的澄清。非常感谢您的回答。但是可以编写hasnetint()方法吗?如果使用hasnetint()方法编写,并且从控制台接收输入,扫描仪将循环,直到您传递一个非整数字符(例如字母)或一个标记程序结束的特殊符号。但对你来说,这不是你想要的。在循环条件中使用hasNextInt()的一个实际示例是,当您不是从控制台读取输入,而是从字符串读取输入时-然后扫描仪将解析字符串,直到它具有下一个int并在末尾停止-请参见此处:正是我要查找的内容。谢谢你。@PetarBivolarski很好的解释;我会把这一点放在主要回复中,解释为什么
    hasNextInt()
    不起作用。非常感谢Kousha,我更新了我的答案,并做了详细的解释(在这个过程中,我注意到我上面的评论中也有一个小错误,因为hasNext()将字符串解析到最后,而不是hasNextInt()-它在找到的第一个非int字符上停止/返回false).为什么扫描仪应该保持打开?一般来说,这是一个坏习惯。试试这个。关闭扫描仪。然后在同一程序中,尝试使用
    System.in
    创建一个新程序。问题是,当您关闭扫描仪时,它也会关闭底层输入流(如果存在)。
            // Creating an array by user keyboard input
            Scanner sc = new Scanner(System.in);
    
            System.out.println("Length of array: ");
    
            int len = sc.nextInt();
            int[] numbers = new int[len]; // use len here
    
            System.out.printf("Type in integer elements of array ", numbers.length);
            int index = 0;
            for (index = 0; index < len; index++) { // and use len here
                numbers[index] = sc.nextInt();
            }
            // created method for printing arrays
            printArray(numbers);
            sc.close();
    
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            // Creating an array by user keyboard input
            Scanner sc = new Scanner(System.in);
    
            System.out.print("Length of array: ");
            int n = sc.nextInt();
            int[] numbers = new int[n];
    
            System.out.printf("Type in %d integer elements of array: ", numbers.length);
            int index = 0;
            while (index < n && sc.hasNextInt()) {
                numbers[index++] = sc.nextInt();
            }
            // created method for printing arrays
            printArray(numbers);
        }
    
        static void printArray(int[] numbers) {
            for (int n : numbers) {
                System.out.print(n + " ");
            }
        }
    }
    
    Length of array: 3
    Type in 3 integer elements of array: 10 20 30
    10 20 30