当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Can still enter input even outside loop

问题描述:

Why is that I can still an input even it's outside the while loop?

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

String input = scan.next();

while(true){

}

}

网友答案:

First, you don't need a while loop to take any input and your loop is practically useless since its not being used. Second, That loop is bad since any other loops you might have in your code will be unreachable. unless you specify a way to break the loop using break; or flags e.g.:

boolean myCondition = true;
while(myCondition){}

or use break:

while(true){
   if(whatever){
        break;
   }
}

Then again you don't need any of this since your not dealing with multiple inputs etc. So your answer is NO its working how you told it to work.

分享给朋友:
您可能感兴趣的文章:
随机阅读: