JAVA – 在循环执行10次后结束

huangapple go评论60阅读模式
英文:

JAVA - End a loop after 10 repeats

问题

Scanner guess = new Scanner(System.in);
Random hundred = new Random();
System.out.println("欢迎来到神奇数字游戏!");
System.out.println("我正在想一个介于0-100之间的数字。您有10轮机会来猜测正确的数字。");
System.out.println("祝您好运!");
int number = hundred.nextInt(101);
int attempts = 0;
while (attempts < 10) {
    System.out.println("您猜测的数字是多少?");
    int guess1 = guess.nextInt();
    if (guess1 > number) {
        System.out.println("您猜测的数字比我想的要大!");
    } else if (guess1 < number) {
        System.out.println("您猜测的数字比我想的要小!");
    } else if (guess1 == number) {
        System.out.println("恭喜您!您猜对了!");
        break;
    }
    attempts++;
}

if (attempts == 10) {
    System.out.println("很遗憾,您没有在10次内猜对。正确的数字是:" + number);
}

System.out.println("再次开始游戏?(是/否)");
String playAgain = guess.next();
if (playAgain.equalsIgnoreCase("是")) {
    // 重新开始游戏的代码
}

guess.close();

请注意,代码中的注释(//)表示了我所添加的说明。如果您需要重新开始游戏,您可以在注释部分添加适当的代码。

英文:

i'm writing a game where the user needs to guess a number between 0-100. the user has 10 tries to guess the right number and after every input he needs to get back a comment.
so i wrote the code and everything works pretty good but i have a problem with breaking the loops after 10 wrong guesses and starting the game again once the game ends.
anything would help. thank you in advace!

Scanner guess = new Scanner (System.in);
Random hundred = new Random ();
System.out.println (&quot;Welcome to Magic Number!&quot;);
System.out.println (&quot;I&#39;m thinking about a number between 0-100. You have 10 rounds to guess the right number.&quot;);
System.out.println (&quot;Good Luck!&quot;);
int number = hundred.nextInt(101);
while (true) {
	System.out.println (&quot;What is the number i&#39;m thinking about?&quot;);
	int guess1 = guess.nextInt();
	 if (guess1 &gt; number) {
		System.out.println (&quot;The number you gueesed is higher than the number i&#39;m thinking about!&quot;); 
	} else if (guess1 &lt; number) {
		System.out.println (&quot;This number you guessed is lower than the number i&#39;m thinking about!&quot;);
	} else if (guess1 == number) {
		System.out.println (&quot;Congratulations! You&#39;ve read my mind!&quot;);
		break;
	}
	
}

答案1

得分: 1

你需要在 while 循环内部添加 numberOfTimes 变量。看一下下面的代码:

int numberOfTimes = 10;

while (numberOfTimes > 0) {
    System.out.println("我在想一个什么数字呢?");
    int guess1 = guess.nextInt();
    if (guess1 > number) {
        System.out.println("你猜的数字比我想的要大!");
    } else if (guess1 < number) {
        System.out.println("你猜的数字比我想的要小!");
    } else if (guess1 == number) {
        System.out.println("恭喜!你猜中了我的心思!");
        break;
    }
    numberOfTimes--;
}
英文:

You have to add numberOfTimes inside your while loop
look at the code below:

int numberOfTimes = 10;

while (numberOfTimes &gt; 0) {
    System.out.println (&quot;What is the number i&#39;m thinking about?&quot;);
    int guess1 = guess.nextInt();
     if (guess1 &gt; number) {
        System.out.println (&quot;The number you gueesed is higher than the number i&#39;m thinking about!&quot;); 
    } else if (guess1 &lt; number) {
        System.out.println (&quot;This number you guessed is lower than the number i&#39;m thinking about!&quot;);
    } else if (guess1 == number) {
        System.out.println (&quot;Congratulations! You&#39;ve read my mind!&quot;);
        break;
    }
    numberOfTimes--;
}

huangapple
  • 本文由 发表于 2020年10月9日 05:43:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64271046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定