使用随机包 (Random package)

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

Using Random package

问题

import java.util.Scanner;
import java.util.Random; 
public class Main {
    static Scanner read = new Scanner(System.in);
    public static void main(String[] args) {
        int randomInt = new Random().nextInt(1000);
        int userInput = -1;
        System.out.println("我猜了一个数字\n轮到你了:");

        while (randomInt != userInput) {
            userInput = read.nextInt();
            if (randomInt > userInput) {
                System.out.println("比它小");
            } else if (randomInt < userInput){
                System.out.println("比它大");
            }
        }
        System.out.println("正确了!");
    }
}
英文:

Okay, I want to create a simple game. I input a number, which was generated by PC using Random package and if I guess it, game over. But! I've no idea what is wrong with it.

import java.util.Scanner;
import java.util.Random; 
public class Main {
    static Scanner read = new Scanner(System.in);
    public static void main(String[] args) {
        int randomInt = new Random().nextInt(1000);
        int userInput = -1;
        System.out.println(&quot;I guessed a number\nYour turn: &quot;);

        while (randomInt != userInput) {
            userInput = read.nextInt();
            if (randomInt &gt; userInput) {
                System.out.println(&quot;Less than it&quot;);
            } else if (randomInt &lt; userInput){
                System.out.println(&quot;More than that&quot;);
            }
        }
            System.out.println(&quot;That&#39;s right!&quot;);
        }
    }

I used Debug and program worked. I mean, Random did his job, generated a number, but then it didn't show me "That's right!" output when I guessed a number. It just goes like "More that that" and "More that that"...

答案1

得分: 2

我自己运行了它,它按预期运行。也许混淆来自于您交换了打印“More than that”和“Less than it”的位置。它应该像这样:

while (randomInt != userInput) {
    userInput = read.nextInt();
    if (randomInt > userInput) {
        System.out.println("More than that");
    } else if (randomInt < userInput) {
        System.out.println("Less than it");
    }
}
英文:

I ran it myself and it runs as expected. Maybe the confusion comes from the fact that you've switched up the places where you print "Less than it" and "More than that".
It should be like

     while (randomInt != userInput) {
        userInput = read.nextInt();
        if (randomInt &gt; userInput) {
            System.out.println(&quot;More than that&quot;);
        } else if (randomInt &lt; userInput){
            System.out.println(&quot;Less than it&quot;);
        }
    }

答案2

得分: 0

if语句部分有错误。另外,应该使用read.close()关闭扫描器以防止内存泄漏。由于没有将扫描器放在主类中的原因,我还将其移到main函数中。现在应该可以正常工作。

import java.util.Scanner;
import java.util.Random;
public class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        int randomInt = new Random().nextInt(1000);
        int userInput = -1;
        System.out.println("我猜了一个数字\n轮到你了:");

        while (randomInt != userInput) {
            System.out.println(randomInt);
            userInput = read.nextInt();
            if (randomInt > userInput) {
                System.out.println("比它更多");
            }
            if (randomInt < userInput){
                System.out.println("比它更少");
            }
        }
        System.out.println("正确!");
        read.close();
    }
}
英文:

You got something wrong with the if statements. Also the scanner should be closed with read.close() to prevent memory leaks. Since there is no reason to put the scanner in the main class I also moved that to the main function. This should work for you know.

import java.util.Scanner;
import java.util.Random;
public class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        int randomInt = new Random().nextInt(1000);
        int userInput = -1;
        System.out.println(&quot;I guessed a number\nYour turn: &quot;);

        while (randomInt != userInput) {
            System.out.println(randomInt);
            userInput = read.nextInt();
            if (randomInt &gt; userInput) {
                System.out.println(&quot;More than it&quot;);
            }
            if (randomInt &lt; userInput){
                System.out.println(&quot;Less than that&quot;);
            }
        }
        System.out.println(&quot;That&#39;s right!&quot;);
        read.close();
    }
}

答案3

得分: 0

"More than that"这个控制台消息在randomInt小于用户输入(randomInt < userInput)时打印出来。如果它大于用户输入,你难道不希望它打印出来吗?另一个消息则相反的条件适用。这可能是问题吗?否则,正如之前建议的那样,使用一个打印语句来输出数字,以便测试正确的猜测。

英文:

The console message "More than that" prints if randomInt is less than user input (randomInt &lt; userInput). Wouldn't you want it to print that if it is more than user input? The inverse condition applies to the other message. Could this be the issue? Otherwise, as already suggested, use a print statement to print the number out so you can test the correct guess.

答案4

得分: 0

我尝试了你的代码,基本上它可以工作。我唯一看到的问题是你打印了错误的字符串。

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

public class Test {

    static Scanner read = new Scanner(System.in);

    public static void main(String[] args) {
        int randomInt = new Random().nextInt(10);
        int userInput = -1;
        System.out.println("我猜了一个数字\n轮到你了:");

        while (randomInt != userInput) {
            userInput = read.nextInt();
            if (randomInt > userInput) {
                System.out.println("比它大");
            } else if (randomInt < userInput) {
                System.out.println("比它小");
            }
        }
        System.out.println("正确了!");
    }
}

一些建议:

  1. 不要在测试中使用如此大的数字。
  2. 使用do while循环而不是while循环。
英文:

I tried your code, and basically it works. The only problem I see is that you print the wrong string.

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

public class Test {

    static Scanner read = new Scanner(System.in);

    public static void main(String[] args) {
        int randomInt = new Random().nextInt(10);
        int userInput = -1;
        System.out.println(&quot;I guessed a number\nYour turn: &quot;);

        while (randomInt != userInput) {
            userInput = read.nextInt();
            if (randomInt &gt; userInput) {
                System.out.println(&quot;More than it&quot;);
            } else if (randomInt &lt; userInput) {
                System.out.println(&quot;Less than that&quot;);
            }
        }
        System.out.println(&quot;That&#39;s right!&quot;);
    }
}

Some tips: <br>

  1. Do not use such big numbers for tests
  2. Instead of using a while loop use a do while loop

huangapple
  • 本文由 发表于 2023年2月6日 18:20:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360002.html
匿名

发表评论

匿名网友

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

确定