2014-09-30 56 views
-4

你好我正在用Java做一個命令系統。舉個例子:你輸入「date」,你會看到時間和日期,但是這個問題出現了,當我試圖修復它時,它是一個永無止境的循環。這裏是我的代碼說明一下:Java Socket永無止境

switch(string){ 
case "date": 
    //Give date 

case "random": 
    //The thing is when you type "random" it will wait on the next input from the user and store it in this variable. 

    while(!string2.equals("BREAK")){ 
     String string2 = (String) inputdata.readObject(); 
     //It will process the users next information if the user NOT type "BREAK". 
    } 
    break; 

    //The problem is that the variable "string2" must be outside the while loop BUT it NEEDS in the loop. I tried with do while loop but the same problem happend. 
} 

如果我必須解決這個問題,我必須做的循環迴路中循環的迴路循環中.... 感謝您的回答。 :D如果你不明白,我不明白我的問題在開始。

+0

沒有你在compairing試圖equalsIgnoreCase ......有時候你爲什麼用'的readObject()'來讀取用戶輸入的信息occours由於區分大小寫 – kirti 2014-09-30 15:38:10

+0

? – 2014-09-30 15:40:44

+0

爲什麼不readObject(),我應該使用什麼? – Tor 2014-09-30 15:44:12

回答

1

你可以在循環外聲明string2然後在裏面引用它嗎?

String string2; 
switch(string){ 
case "date": 
//Give date 

case "random": 
//The thing is when you type "random" it will wait on the next input from the user and store it  in this variable. 

while(!string2.equals("BREAK")){ 
    string2 = (String) inputdata.readObject(); 
    //It will process the users next information if the user NOT type "BREAK". 
} 
break; 

//The problem is that the variable "string2" must be outside the while loop BUT it NEEDS in the loop. I tried with do while loop but the same problem happend. 
}