简体   繁体   English

Java - 扫描仪不要求输入

[英]Java - Scanner not asking for a input

System.out.println("Please enter the amount of money you have here: ");
Scanner have = new Scanner(System.in);
System.out.println("Please enter your starting bet here: ");
Scanner rate = new Scanner(System.in);

int moneyHad = Integer.parseInt(have.next());
int moneyRate = Integer.parseInt(rate.next());
System.out.println(moneyHad + ", " + moneyRate);

There is my code, and this is my output. 有我的代码,这是我的输出。

Please enter the amount of money you have here: 
Please enter your starting bet here: 1
1
1, 1

As you can see it prints them both before it asks, thats why there was no input for line 1 of the output. 正如你所看到的那样,它会在它询问之前打印它们,这就是为什么输出的第1行没有输入。

Please help me quickly! 请快点帮我!

  • No need to create 2 Scanner Object 无需创建2个扫描程序对象
  • There is a method that returns int ( scanner.nextInt() ) no need to ParseInt 有一个方法返回int(scanner.nextInt()),不需要ParseInt
  • The input is red when you call the scanner.nextInt() not when creating a Scanner Object 当您在创建扫描程序对象时调用scanner .nextInt()时,输入为红色

Try this : 尝试这个 :

Scanner scanner = new Scanner(System.in);

        System.out.print("Please enter the amount of money you have here: ");
        int moneyHad = scanner.nextInt();
        System.out.print("Please enter your starting bet here: ");
        int moneyRate = scanner.nextInt();


        System.out.println(moneyHad + ", " + moneyRate);

你只需要一个扫描仪对象并调用nexInt()方法来获取后面的条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM