0
我是一個初學者程序員,我創建了一個方法,它接受用戶輸入的字符串,檢查它是整數還是字符,將其轉換並將其傳遞到方法的下一部分這是檢查存儲在鏈表中的Person對象的實例的id值的每個循環的一個循環。對於每個無限循環
然而,它創造了一個無限循環,我不知道如何打破它,所以它只輸出一次。任何援助非常感謝。
我只包括我相信是問題的方法,如果您需要更多的程序部分,請讓我知道,我會添加它。
public void theSiv() {
System.out.println("Please enter client Id:");
String s = In.nextLine();
boolean isValidInteger = false;
char choice = 'p';
boolean exists = false;
int searchid = 0;
try
{
int i = Integer.parseInt(s);
isValidInteger = true;
searchid = i;
}
catch (NumberFormatException ex)
{
choice = s.charAt(0);
}
while (choice !='x'){
for (Person b:clients)
if (b.getId() == searchid){
exists = true;
System.out.println("found client");}
if(exists == false)
System.out.println(" No such client");
}
}
順便說一句,你應該使用'{'和'}'找出更好的範圍。例如,您的「找到的客戶端」將始終顯示。 – 2015-04-04 23:56:43