Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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(true)问题_Java_Loops_While Loop_Exception Handling_Inputmismatchexception - Fatal编程技术网

与异常处理相关的基本Java While(true)问题

与异常处理相关的基本Java While(true)问题,java,loops,while-loop,exception-handling,inputmismatchexception,Java,Loops,While Loop,Exception Handling,Inputmismatchexception,在过去的一个小时里,我研究了while(true)循环,但我找不到关于这个循环的答案 public class Test { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { System.out.println("Enter Integer: "); int i = GetAnInteger(); System.

在过去的一个小时里,我研究了
while(true)
循环,但我找不到关于这个循环的答案

public class Test {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("Enter Integer: ");
        int i = GetAnInteger();
        System.out.println("You entered: " + i);
    }

    public static int GetAnInteger() {
        while (true) {
            try {
                return sc.nextInt();
            }

            catch (InputMismatchException e) {
                sc.next();
                System.out.println("That's not an Integer, try again: ");
            }
        }
    }
}
问题1:我们知道代码中的所有语句都将由编译器运行。“while(true)”的目的是确保代码可以无限期地运行,方法中的代码将被执行,那么为什么我们首先需要while(true)循环呢

问题2:如果我删除“while(true)”语句,IDE会要求我创建一个return语句或将该方法设置为void,为什么?在这种情况下,“while(true)”是如何工作的

关于堆栈溢出的其他帖子大多在争论为什么“while(true)”是好是坏,我对此不感兴趣。我感兴趣的是,为什么这段代码在没有“while(true)”的情况下会崩溃,以及我如何知道在我的其他代码中何时使用“while(true)”呢

  • 在过去的半个小时里,我搜索了youtube、java complete reference和stack overflow,但没有找到任何答案。这段代码取自《Java for dummies》一书,它没有解释while(true)语句的目的
  • while(true)
    的目的是使其运行,直到输入有效并到达
    return
    语句。当它到达
    return
    语句时,它将退出方法/循环

  • 这是因为不能保证执行
    try
    部分。如果不是,则该方法需要返回一些内容。必须为方法的每个可能分支包含一个return语句。 考虑以下事项:

    try {
         //Oh no! InputMismatchException!!
         return sc.nextInt(); 
         //Goes to catch block
    } catch (InputMismatchException e) {
         sc.next();
         System.out.println("That's not an Integer, try again: ");
         //executes catch block
    }
    //Uh oh. Now what? What gets returned?
    
  • 那么,为什么我们首先需要一个while(true)循环呢

    while
    循环允许用户在输入非整数的内容时“重试”。当我运行您的程序时,我可以执行以下操作:

    Enter Integer: 
    this
    That's not an Integer, try again: 
    is
    That's not an Integer, try again: 
    not
    That's not an Integer, try again: 
    an
    That's not an Integer, try again: 
    integer
    That's not an Integer, try again: 
    1
    You entered: 1
    
    如果我删除'while(true)'语句,IDE会要求我创建一个return语句或将该方法设置为void,为什么?while(true)在这种情况下如何工作

    有了
    while
    循环,就可以保证最终返回一些东西。如果没有
    while
    循环,如果用户没有输入整数,就没有任何返回。因此,您必须在方法的末尾添加一些内容(默认返回值,这破坏了程序的意图),或者您必须使该方法根本没有返回值

    让我们设想一下,我们在循环时删除
    ,并在方法末尾添加
    return 0
    ,以使编译器满意。现在,让我们再次尝试上述场景:

    Enter Integer: 
    this
    That's not an Integer, try again: 
    You entered: 0
    
    显然这不是我们想要的

    虽然(true){}
    是一种反模式,但它是一个无休止的循环,如果您看到它,总会有更好的方法来实现它,就像这里的情况一样,如副本中所述,正确使用
    扫描仪