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循环?而在一个基本的猜谜游戏中循环_Java_Loops_While Loop - Fatal编程技术网

如何退出Java循环?而在一个基本的猜谜游戏中循环

如何退出Java循环?而在一个基本的猜谜游戏中循环,java,loops,while-loop,Java,Loops,While Loop,我正试图写一个小游戏,但我一直在想,如果用户想再次玩,如何提示用户,如果用户不想再次玩,如何退出循环 import java.util.Random; import java.util.Scanner; public class Guessinggame { public static void main(String[] args) { System.out.println("Welcome to guessing game! \n" + " You must guess a n

我正试图写一个小游戏,但我一直在想,如果用户想再次玩,如何提示用户,如果用户不想再次玩,如何退出循环

import java.util.Random;
import java.util.Scanner;

public class Guessinggame {

public static void main(String[] args) {

    System.out.println("Welcome to guessing game! \n" + " You must guess a number between 1 and 100. ");

    while (true) {

        Random randomNumber = new Random();
        Scanner g = new Scanner(System.in);

        int number = randomNumber.nextInt(100) + 1;
        int guess = 0;
        int numberOfGuesses = 0;

        while (guess != number){

            System.out.print("Guess: ");
            guess = g.nextInt();

            if (guess > number ){
                System.out.println( "You guessed too high!");
            }else if (guess < number ){
                System.out.println( "You guessed too low!");
            }else{
                System.out.println( "Correct! You have guessed "+ numberOfGuesses + " times. \nDo you want to play again? (y/n)  ");

            }
            numberOfGuesses++;


        }
    }
}
import java.util.Random;
导入java.util.Scanner;
公开课猜谜游戏{
公共静态void main(字符串[]args){
System.out.println(“欢迎来到猜谜游戏!\n”+“您必须猜一个介于1和100之间的数字。”);
while(true){
随机数=新随机数();
扫描仪g=新的扫描仪(System.in);
整数=随机数。nextInt(100)+1;
int guess=0;
int numberOfGuesses=0;
while(猜测!=数字){
系统输出打印(“猜测:”);
guess=g.nextInt();
如果(猜测>数字){
System.out.println(“你猜得太高了!”);
}else if(猜测<数字){
System.out.println(“你猜得太低了!”);
}否则{
System.out.println(“正确!您已经猜了“+numberofguesss+”次。\n是否要再次播放?(y/n)”;
}
numberOfGuesses++;
}
}
}

}

您可以使用
break
退出当前循环

for (int i = 0; i < 10; i++) {
  if (i > 5) {
    break;
  }
  System.out.Println(i);
}
然而,循环可能更适合您的用例

变化

while(true){
  //At some point you'll need to 
  //exit the loop by calling the `break` key word
  //for example:

  if(/*it's not compatible with your condition*/)
    break;
}

然后询问用户是否仍要播放,如果不想,则将此变量设置为
false

另一个解决方案是保持代码不变,只需调用
break
在您询问用户并且他们说不想继续之后,这只是跳出当前循环并在循环后的第一个点恢复。 这是不太可取的,因为在阅读代码时跟踪程序流会更困难,特别是当您开始有嵌套循环或多个
中断
点时。

您可以使用
do while
语句更改
while(true)
语句

Scanner k= new Scanner(System.in);

do{
// do sth here...

//ask to user for continue or exit
System.out.println("Continue/Break");
String answer = k.next();

}while(answer.equals("Continue"));

如果要退出循环,可以使用
break
语句。

退出循环的最佳选项是使用“break”。所以,做一个条件(如果)并检查用户是否想再次播放,如果不想,则返回break
Scanner k= new Scanner(System.in);

do{
// do sth here...

//ask to user for continue or exit
System.out.println("Continue/Break");
String answer = k.next();

}while(answer.equals("Continue"));
Scanner k= new Scanner(System.in);

do{
// do sth here...

//ask to user for continue or exit
System.out.println("Continue/Break");
String answer = k.next();

}while(answer.equals("Continue"));