我希望我的用戶繼續輸入員工編號,直到用戶輸入-1,這會停止循環。但我的代碼不斷打印輸入的數字,不會再提出另一個問題。保持要求用戶輸入數據,陷入無限循環
System.out.println("Which employee number do you want to see -->");
index = myScanner.nextInt();
while (payData.getOneEmpNum(index) != -1)
{
if (payData.getOneEmpNum(index) >= 0)
{
System.out.printf("Sequential found employee #%d ",payData.getOneEmpNum(index));
System.out.printf("and the pay rate is $%.2f.\n ", payData.getPayRate(index));
System.out.printf("Binary found employee #%d ",payData.getOneEmpNum(index));
System.out.printf("and the pay rate is $%.2f.\n ", payData.getPayRate(index));
}
else
{
System.out.println("Invalid employee number!");
}
System.out.println("Which employee number do you want to see -->");
}
我沒有看到你要的輸入'而()'循環內?什麼會導致'payData.getOneEmpNum(index)'改變,導致循環中斷? – mash 2014-12-07 04:28:31
得到循環內的下一個輸入 ** index = myScanner.nextInt(); ** – 2014-12-07 04:34:18