Java - 扫描仪不要求输入

时间:2016-05-19 13:04:30

标签: java java.util.scanner

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);

有我的代码,这是我的输出。

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

正如你所看到的那样,它会在它询问之前打印它们,这就是为什么输出的第1行没有输入。

请快点帮助我!

2 个答案:

答案 0 :(得分:2)

  • 无需创建2个扫描程序对象
  • 有一个返回int的方法(scanner.nextInt())不需要ParseInt
  • 当您在创建扫描程序对象时调用scanner .nextInt()时输入为红色

试试这个:

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);

答案 1 :(得分:1)

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