简体   繁体   中英

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.

Please help me quickly!

  • No need to create 2 Scanner Object
  • There is a method that returns int ( scanner.nextInt() ) no need to ParseInt
  • The input is red when you call the scanner.nextInt() not when creating a Scanner Object

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()方法来获取后面的条目。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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