问题描述
我在实施你想要再次玩的问题然后如果玩家进入Y然后重新启动整个游戏。我只需要添加另一个while语句,当我这样做时就把它搞砸了。
我尝试了什么:
包journal.pkg3c;
/ **
*
* @author stephenwessels
* /
import java.util.Scanner;
import java.util.Random;
公共类Journal3C
{
/ **
* @param args the the命令行参数
* /
public static void main(String [] args)
{
Scanner in = new Scanner(System.in);
Random rnd = new Random();
System.out.println(猜一个1到10之间的数字);
int count = 1;
int guess = in.nextInt();
int num = rnd .nextInt(10)+ 1;
while(guess!= num)
{
系统。 Ø ut.println(猜一个1到10之间的数字);
guess = in.nextInt();
count ++;
}
System.out.println(需要+计数+猜测才能正确猜测);
System.out。 println(你想再玩Y / N吗?);
}
}
I'm having issues with implementing a would you like to play again and then if the player enters Y then it restarts the whole game. I just need to add another while statement in when I do it just messes it all up.
What I have tried:
package journal.pkg3c;
/**
*
* @author stephenwessels
*/
import java.util.Scanner;
import java.util.Random;
public class Journal3C
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Random rnd = new Random();
System.out.println("Guess a number between 1 and 10");
int count = 1;
int guess = in.nextInt();
int num = rnd.nextInt(10) + 1;
while(guess != num)
{
System.out.println("Guess a number between 1 and 10");
guess = in.nextInt();
count++;
}
System.out.println("It took " + count + " guesses to guess correctly");
System.out.println("Would you like to play again Y/N?");
}
}
推荐答案
Boolean play = true;
while (play) {
// game code here
System.out.println("Would you like to play again Y/N?");
play = in.nextLine().trim().equalsIgnoreCase("y"); // "y" will set 'play' to 'true', anything else will set it to 'false'
}
这篇关于创建一个你想再玩Y / N?选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!