Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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
Java While循环终止_Java_While Loop - Fatal编程技术网

Java While循环终止

Java While循环终止,java,while-loop,Java,While Loop,如果我注释掉行garbage=scan.nextLine(),while循环无限运行。否则,它不会。我理解如果只有print命令,为什么它会无限运行,但我不完全理解包含垃圾变量是如何阻止它无限运行的。有人能解释一下吗 import java.util.Scanner; 公共类TypeSafeReadInteger { 公共静态void main(字符串[]args) { 扫描仪扫描=新扫描仪(System.in); 字符串垃圾; System.out.print(“将年龄输入为整数>”; 而(!

如果我注释掉行
garbage=scan.nextLine(),while循环无限运行。否则,它不会。我理解如果只有print命令,为什么它会无限运行,但我不完全理解包含
垃圾
变量是如何阻止它无限运行的。有人能解释一下吗

import java.util.Scanner;
公共类TypeSafeReadInteger
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
字符串垃圾;
System.out.print(“将年龄输入为整数>”;
而(!scan.hasnetint())
{
垃圾=scan.nextLine();
System.out.print(“\n请输入一个整数>”);
}
int age=scan.nextInt();
System.out.println(“您的年龄是”+年龄);
}
}

垃圾
只是一个变量,“停止”while循环的是
nextLine()
它是一个等待用户输入的方法。直到用户使用键盘输入内容并将输入保存到
垃圾
变量中,while才会继续。垃圾
只是一个变量,while循环的“停止”是
nextLine()
它是一个等待用户输入的方法。直到用户使用键盘输入内容并将输入保存到
垃圾
变量中,while才会继续。要修复代码,请执行以下操作:

    while (!scan.hasNextInt())
    {
        scan.nextLine(); // the order of the lines inside the loop makes the difference!
        System.out.print("\nPlease enter an integer > ");
        // when you perform nextLine() here - you reach the beginning of the loop 
        // without a token in the scanner - so you end up looping forever
    }
    int age = scan.nextInt();
顺便说一句,正如您从上面的示例中看到的,
垃圾
是多余的

要修复您的代码:

    while (!scan.hasNextInt())
    {
        scan.nextLine(); // the order of the lines inside the loop makes the difference!
        System.out.print("\nPlease enter an integer > ");
        // when you perform nextLine() here - you reach the beginning of the loop 
        // without a token in the scanner - so you end up looping forever
    }
    int age = scan.nextInt();

顺便说一句,正如您从上面的示例中看到的,
垃圾
是多余的

如果用户输入一个整数,则一切正常。如果没有,那么您将得到无限循环,而不使用
garbage=scan.nextLine()

当您执行类似于
scan.hasnetint()的操作时,实际上没有从输入中读取任何字符。因此,如果用户输入类似于“cat”的内容以响应您的提示,那么输入将在该单词的第一个字母之前暂停。由于循环直到输入中有一个整数,因此不会再读取任何内容,并且将无限循环,因为“cat”正好位于输入缓冲区中


通过添加
scan.nextLine()
,您将使扫描仪放弃用户点击之前的所有内容,并可以处理其他输入。

如果用户输入整数,则所有内容都可以工作。如果没有,那么您将得到无限循环,而不使用
garbage=scan.nextLine()

当您执行类似于
scan.hasnetint()的操作时,实际上没有从输入中读取任何字符。因此,如果用户输入类似于“cat”的内容以响应您的提示,那么输入将在该单词的第一个字母之前暂停。由于循环直到输入中有一个整数,因此不会再读取任何内容,并且将无限循环,因为“cat”正好位于输入缓冲区中


通过添加
scan.nextLine()
,您将导致扫描仪放弃所有内容,直到用户点击并可以处理其他输入。

您需要知道两件事:

  • hasNextLine()
    不会推进扫描仪实例
  • nextLine()
所谓“推进扫描仪实例”,我指的是“消耗”输入。将输入视为流,并将扫描仪对象视为正在使用该流的对象


正常流中的东西只能消费一次。您在一个名为
垃圾
的变量中捕获了您的,但是您也可以轻松地调用
scan.nextLine()
,而不存储结果。我强烈建议您阅读,看看哪些方法可以提升扫描仪实例,哪些不可以。

您需要知道两件事:

  • hasNextLine()
    不会推进扫描仪实例
  • nextLine()
所谓“推进扫描仪实例”,我指的是“消耗”输入。将输入视为流,并将扫描仪对象视为正在使用该流的对象


正常流中的东西只能消费一次。您在一个名为
垃圾
的变量中捕获了您的,但是您也可以轻松地调用
scan.nextLine()
,而不存储结果。我强烈建议您阅读,看看哪些方法推进了扫描仪实例,哪些方法没有。

如果某个方法是真的,而您不做任何更改,它会变为假吗?如果某个方法是真的,而您不做任何更改,它会变为假吗?就无限循环而言,答案是错误的。在这方面,
nextLine()
next()
的行为方式相同。就无限循环而言,答案是错误的。在这方面,
nextLine()
next()
的行为方式相同。