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

为了检查用户输入的值并查看计算出的随机数,我想让这些数字投影在控制台中(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();


    }   


}


慕娘9325324
浏览 81回答 3
3回答

隔江千里

你可以做这样的事情。// codeScanner Invoermax = new Scanner(System.in);System.out.println("Under which number you want to guess"); int Invoer = Invoermax.nextInt();Invoermax.nextLine(); // reading empty space leftint Hoogte = ThreadLocalRandom.current().nextInt(1,Invoermax.nextInt()); // code

长风秋雁

您有两个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 

慕侠2389804

每次您调用时,Scanner.nextInt()它都会等待您的输入。问题是你调用它两次,替换:    int Hoogte = ThreadLocalRandom.current().nextInt(1,Invoermax.nextInt());使用您已经获得的变量:    int Hoogte = ThreadLocalRandom.current().nextInt(1,Invoer);顺便说一句,通常在 java 中,字段/变量名称以小写字母开头,所以应该是hoogte,等。inoverMaxinover
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java