为了账号安全,请及时绑定邮箱和手机立即绑定

我尝试做一个猜谜游戏,让用户决定要猜的最大数字

我尝试做一个猜谜游戏,让用户决定要猜的最大数字

慕娘9325324 2023-09-13 18:05:22
为了检查用户输入的值并查看计算出的随机数,我想让这些数字投影在控制台中(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();    }   }
查看完整描述

3 回答

?
隔江千里

TA贡献1906条经验 获得超10个赞

你可以做这样的事情。


// code

Scanner Invoermax = new Scanner(System.in);

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

int Invoer = Invoermax.nextInt();

Invoermax.nextLine(); // reading empty space left

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

// code


查看完整回答
反对 回复 2023-09-13
?
长风秋雁

TA贡献1757条经验 获得超7个赞

您有两个Scanner.nextInt()调用,因此有两个输入读数,两个输入等待。


    int Invoer = Invoermax.nextInt();   // the first input reading

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

                  Invoermax.nextInt());  // the second input reading

当您在控制台(任何类型)中输入两个 int 值时,您将看到结束打印行。


如果您的设计只有一个输入,则使用兑现值进行第二次使用


    int Invoer = Invoermax.nextInt();   // the first input reading

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

                  Invoer );             // use cashed value 


查看完整回答
反对 回复 2023-09-13
?
慕侠2389804

TA贡献1719条经验 获得超6个赞

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

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

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

使用您已经获得的变量:

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

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


查看完整回答
反对 回复 2023-09-13
  • 3 回答
  • 0 关注
  • 76 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信