-1
我有這個類的問題(對於真正混亂的代碼抱歉)。 while(scan.hasNextLine())代碼用循環完成後,沒有任何反應。代碼只是凍結。 Java不報告任何錯誤,但它應該繼續運行。Java while while循環錯誤,非語法
類是服務器的實現,它使用來自套接字的Rock,Paper或Scissors收集消息,隨機地做出它自己的決定並將其發送回客戶端。
while (keepRunning == true) {
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuffer stringBuffer = new StringBuffer();
String line = null;
Scanner scan = new Scanner(br);
while (scan.hasNextLine()) {
line = scan.nextLine();
stringBuffer.append(line);
stringBuffer.append("\n");
listForServer.add(line);
System.out.println(line);
if (line.contentEquals("SHAPE") == false) {
counter = counter + 1;
}
Random rn = new Random();
int n = 2 - 0 + 1;
int i = rn.nextInt() % n;
int randomNum = 0 + i;
if (randomNum == 0) {
shape = "Rock";
randServerList.add(shape);
} else if (randomNum == 1) {
shape = "Paper";
randServerList.add(shape);
} else {
shape = "Scissors";
randServerList.add(shape);
}
}
scan.close();
System.out.println("Shapes are chosen");
System.out.println("Client has send " + (counter - 1) + "shapes");
}
請將代碼縮小到相關部分,而不是發佈整個程序。你的調試器說什麼? –
'while(keepRunning = true)''='是賦值運算符,'=='是比較。爲了避免這種錯誤,只需使用'while(keepRunning)' – Pshemo
感謝提示。調試器沒有說什麼,他應該打印「選擇形狀」的消息,但是他只是在while循環之後停止。 – tijetof