gpt4 book ai didi

java - 为什么代码无法访问?

转载 作者:行者123 更新时间:2023-12-01 16:59:21 28 4
gpt4 key购买 nike

以下内容在“out.close();”上给我一条“代码无法访问”消息我找不到问题,因为它与我运行过的其他有效代码或多或少相同!

import java.io.*;
import java.net.*;

public class MyClient {
private static String SERVER = "127.0.0.1";
private static Integer PORT = 8765;
public static void main(String[] args) throws IOException {
// Connect to the server and create the writer and reader
Socket socket = new Socket(SERVER,PORT);
PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// Loop forever
while(true) {

out.println("Question:");
String sum = System.console().readLine();
out.println(sum);

String line = in.readLine().trim();
if(line==null || line.startsWith("Finished")) {
socket.close();
return;
}
else if (line.startsWith("My answer is: ")){
System.out.println(line);
String message = System.console().readLine();//correct or wrong!!
out.println(message);
}
}
// Close the in and out and socket
out.close();
in.close();
socket.close();
}
}

最佳答案

您正在从 while 循环中执行return。您应该改为break

关于java - 为什么代码无法访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28915701/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com