Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么代码无法访问?_Java_Unreachable Code - Fatal编程技术网

Java 为什么代码无法访问?

Java 为什么代码无法访问?,java,unreachable-code,Java,Unreachable Code,下面是关于'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(Strin

下面是关于'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
循环中执行
返回。您应该改为执行
break

您在
while
循环中执行
返回。你应该改为中断

问题出在这里

 // Loop forever
        while(true) {
它将永远循环,您永远不会停止它,因此循环后的下一行将永远不会执行。就是这样:问题出在这里

 // Loop forever
        while(true) {

它将永远循环,您永远不会停止它,因此循环后的下一行将永远不会执行。就是这样:P

因为你有一个无限循环(

while(true)
),没有中断或其他退出方式

因为您有一个无限循环(
while(true)
),没有中断或其他退出方式

在循环中执行
返回
不是一种好的方式,但是如果您想确保释放资源,可以使用
尝试来包装您的循环。。。最后

try {
  while(true) {
    // ...
    if(condition) {
      return;
    }
    // ...
  }
} finally {
  out.close(); // this is called just before leaving the surrounding function
  // ...
}

即使在循环中抛出异常,这种方法也有效。

在循环中执行
返回
不是一种好的方式,但是如果要确保释放资源,可以使用
try来包装循环。。。最后

try {
  while(true) {
    // ...
    if(condition) {
      return;
    }
    // ...
  }
} finally {
  out.close(); // this is called just before leaving the surrounding function
  // ...
}

即使在循环中抛出异常,这也可以工作。

因为代码永远不会到达:

    // Close the in and out and socket
    out.close();
    in.close();
    socket.close();
返回
更改为
中断

    if(line==null || line.startsWith("Finished")) {
        socket.close();
        break; //<------------------CHANGE
    }
if(line==null | | line.startsWith(“Finished”)){
socket.close();

中断;//,因为代码永远无法到达:

    // Close the in and out and socket
    out.close();
    in.close();
    socket.close();
返回
更改为
中断

    if(line==null || line.startsWith("Finished")) {
        socket.close();
        break; //<------------------CHANGE
    }
if(line==null | | line.startsWith(“Finished”)){
socket.close();

break;//return必须是breakreturn必须是break