2016-05-19 65 views
1
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); 

有我的代码,这是我的输出。Java - 扫描器不要求输入

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

正如你可以看到它打印它们都在它要求之前,这就是为什么没有输入第1行的输入。

请快点帮帮我吧!

+0

你为什么要创建两个'Scanner'对象? – Abubakkar

+1

只要看看代码行的顺序,并考虑每行代码的作用。或者使用调试器,以获得更深入的洞察力。一旦你完成了这个任务,就不应该太难找到问题所在。并且'Scanner'上的提示:在同一个'InputStream'上的多个扫描仪很可能会导致一些问题,并且首先不需要两个扫描仪。 – Paul

+0

Abubakkar Rangara,因为我需要2个输入。 – mobinblack

回答

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

但它仍然在做... – mobinblack

+0

加你为什么只需要一个扫描仪。 – mobinblack

+0

非常感谢你 – mobinblack

1

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

+0

是的,我在上面的评论中看到了。 – mobinblack