首页 > 解决方案 > 我试着做一个猜谜游戏,让用户决定猜的最大数字是多少

问题描述

为了检查用户输入的值并查看计算出的随机数,我想让这些数字投影在控制台(eclipse)中。但是什么勾勒了我的惊讶?输入数字后,最后两个 system.out.println(就在 invoermax.close()) 上方)没有出现在控制台屏幕中????这就像java甚至没有阅读或注意到这些代码行,怎么会???

这是我的代码:

package Package1;

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;


public class test6KOPIE1 {


    public static void main(String[] args)
    {
        Scanner Invoermax = new Scanner(System.in);


        System.out.println("Under which number you want to guess");

        int Invoer = Invoermax.nextInt();


        int Hoogte = ThreadLocalRandom.current().nextInt(1,Invoermax.nextInt());



        System.out.println("So a guess under max: " + Invoer);

        System.out.println("The random number has become " + Hoogte);

        Invoermax.close();

    }   

}

标签: javaeclipse

解决方案


每次您调用Scanner.nextInt()它都会等待您的输入。

问题是你调用它两次,替换:

    int Hoogte = ThreadLocalRandom.current().nextInt(1,Invoermax.nextInt());

使用您已经获得的变量:

    int Hoogte = ThreadLocalRandom.current().nextInt(1,Invoer);

顺便说一句,通常在 java 中,字段/变量名称以小写字母开头,所以应该是hoogte,inoverMaxinover


推荐阅读