随机数猜测游戏无限循环

huangapple 未分类评论64阅读模式
标题翻译

Random # Guessing Game Infinite Loop

问题

/*
  This program will generate a random number. 
  It will ask the user to guess what number was generated and say
  if the guess is too high or low.
*/

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

public class RandomNumGame {
    public static void main(String[] args) {
        Random rand = new Random();
        Scanner input = new Scanner(System.in);
        int randNum = rand.nextInt(20);
        System.out.println("Number is : " + randNum);
        int userGuess = 0;
        int success = 0;
        System.out.println("Guess the number: ");
        userGuess = input.nextInt();
        
        while(success == 0) {
            if(userGuess > randNum) {
                System.out.println("Too high");
            }
            else if(userGuess < randNum) {
                System.out.println("Too low");  
            }
            else {
                System.out.println("Something is very wrong."); 
            }
        }
        
        if(userGuess == randNum) {
            success++;
            System.out.println("You got it! Play again?");
        }
    }
}
英文翻译

For my Java class, I'm supposed to make a random number guessing game. I've been stuck on a loop I created for the past couple of days. The output of the program is always an infinite loop and I can't see why. Any help is very much appreciated.

/*
  This program will generate a random number. 
  It will ask the user to guess what number was generated and say
  if the guess is too high or low.

*/

import java.util.Scanner;

import java.util.Random;

public class RandomNumGame {
    public static void main(String[] args) {
        

        Random rand = new Random();
        Scanner input = new Scanner(System.in);
        int randNum = rand.nextInt(20);
        System.out.println(&quot;Number is : &quot; + randNum);
        int userGuess = 0;
        int success = 0;
        System.out.println(&quot;Guess the number: &quot;);
        userGuess = input.nextInt();
        
        while(success == 0)
        {
        
            if(userGuess &gt; randNum){
                System.out.println(&quot;Too high&quot;);
            }
            else if(userGuess &lt; randNum){
                System.out.println(&quot;Too high&quot;);  
            }
            else{
                System.out.println(&quot;Something is very wrong.&quot;); 
            }

        }
            if(userGuess == randNum){
                success++;
                System.out.println(&quot;You got it! Play again?&quot;);
            }
        
    }
}

</details>


# 答案1
**得分**: 0

你将检查输入是否等于数字的**if**语句放在了while循环的外面,所以循环永远不会结束。

以下是修正后的代码:

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

public class RandomNumGame {
    public static void main(String[] args) {
        Random rand = new Random();
        Scanner input = new Scanner(System.in);
        int randNum = rand.nextInt(20);
        System.out.println("数字是:" + randNum);
        int userGuess = 0;
        int success = 0;
        System.out.println("猜数字:");
        userGuess = input.nextInt();

        while(success == 0) {
            if(userGuess > randNum) {
                System.out.println("太高了");
            } else if(userGuess < randNum) {
                System.out.println("太低了");  
            } else if(userGuess == randNum) {
                success++;
                System.out.println("你猜对了!要再玩一次吗?");
            } else {
                System.out.println("出现了一些问题。");
            }
        }
    }
}
英文翻译

You put the if that checks if the input is equal to the number outside the while, so the cycle never ends.

Here is the code fixed:

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

public class RandomNumGame {
    public static void main(String[] args) {
        Random rand = new Random();
        Scanner input = new Scanner(System.in);
        int randNum = rand.nextInt(20);
        System.out.println(&quot;Number is : &quot; + randNum);
        int userGuess = 0;
        int success = 0;
        System.out.println(&quot;Guess the number: &quot;);
        userGuess = input.nextInt();

        while(success == 0) {
            if(userGuess &gt; randNum) {
                System.out.println(&quot;Too high&quot;);
            } else if(userGuess &lt; randNum) {
                System.out.println(&quot;Too high&quot;);  
            } else if(userGuess == randNum) {
                success++;
                System.out.println(&quot;You got it! Play again?&quot;);
            } else {
                System.out.println(&quot;Something is very wrong.&quot;);
            }
        }
    }
}

答案2

得分: 0

我修复了它!我在每个失败条件后添加了中断,这样我就不会陷入无限循环。我之前尝试过这个,但是一直报错。

以下是完成的代码:

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

public class RandomNumGame {
    public static void main(String[] args) {
        Random rand = new Random();
        Scanner input = new Scanner(System.in);
        int randNum = rand.nextInt(20);
        System.out.println("数字是: " + randNum);
        int userGuess = 0;
        boolean success = false;

        System.out.println("猜测数字: ");
        userGuess = input.nextInt();
        input.close();

        while(success == false){
            if(userGuess > randNum) {
                System.out.println("太高了,请重试");
                break;
            } else if(userGuess < randNum) {
                System.out.println("太低了");
                break;  
            } else if(userGuess == randNum) {
                success = true;
                System.out.println("你猜对了!要再玩一次吗?");
            } else {
                System.out.println("出现了一些严重错误。");
            }
        }
    }
}
英文翻译

I fixed it! I added breaks after each fail condition so that I wouldn't get an infinite loop. I tried this earlier, but I kept getting errors.

This is the completed code:

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

public class RandomNumGame {
    public static void main(String[] args) {
        Random rand = new Random();
        Scanner input = new Scanner(System.in);
        int randNum = rand.nextInt(20);
        System.out.println(&quot;Number is : &quot; + randNum);
        int userGuess = 0;
        boolean success = false;

        System.out.println(&quot;Guess the number: &quot;);
        userGuess = input.nextInt();
        input.close();

        while(success == false){
            if(userGuess &gt; randNum) {
                System.out.println(&quot;Too high,try again&quot;);
                break;
            } else if(userGuess &lt; randNum) {
                System.out.println(&quot;Too low&quot;);
                break;  
            } else if(userGuess == randNum) {
                success = true;
                System.out.println(&quot;You got it! Play again?&quot;);
            } else {
                System.out.println(&quot;Something is very wrong.&quot;);
            }
        }
    }
}

</details>



huangapple
  • 本文由 发表于 2020年3月4日 05:26:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/60515771.html
匿名

发表评论

匿名网友

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

确定