Java while循环错误,非语法
我对这个类有问题(很抱歉代码太乱)。 使用循环完成while(scan.hasNextLine())代码后,不会发生任何事情。代码冻结了。Java不会报告任何错误,但应该继续运行 类是服务器的实现,它使用石头、布或剪刀从套接字收集消息,随机做出自己的决定并将其发送回客户端Java while循环错误,非语法,java,loops,while-loop,freeze,Java,Loops,While Loop,Freeze,我对这个类有问题(很抱歉代码太乱)。 使用循环完成while(scan.hasNextLine())代码后,不会发生任何事情。代码冻结了。Java不会报告任何错误,但应该继续运行 类是服务器的实现,它使用石头、布或剪刀从套接字收集消息,随机做出自己的决定并将其发送回客户端 while (keepRunning == true) { socket = serverSocket.accept(); InputStream is = socket.getInputStream();
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");
}
来自
如果此扫描仪的输入中有另一行,则返回true。此方法在等待输入时可能会阻塞。扫描仪不会超过任何输入
由于循环中有line=scan.nextLine(),并且扫描处于打开状态,因此它将继续等待输入。因此,代码被阻止,您看不到任何输出。请将代码缩小到相关部分,而不是发布整个程序。调试器说什么?在
中,(keepRunning=true)
=
是赋值运算符,=
是比较运算符。要避免此类错误,只需使用while(keepRunning)
谢谢您的提示。调试器并没有说什么,他应该打印“Shapes is Selected”消息,但他只是在while循环之后停止。