字段永远不会分配给,并且始终具有默认值0

时间:2018-03-17 02:22:45

标签: c#

我搜索过类似的结果,并且有几个问题与我的相符,但没有一个问题能给我解决方案。我的代码字段radius1将自己设置为零。我知道为什么,这是因为它在初始类中,因此无论何时调用类,radius1都设置为零。如何将其修复到可以调用其他方法并保持半径相同的位置?这是我的代码:

import java.net.*;
import java.io.*;
import java.util.concurrent.*;

/**
 * Server application which allows multiple clients to connect and execute simple file transfers.
 *
 * Manages a fixed thread pool of maximum 10 threads (connections)
 *
 */
public class Server {

    private static final int port = 8888;
    // As a demonstration, put everything into main(); obviously you would probably want
    // to use instance variables and break this up into separate methods for a real application.
    public static void main(String[] args) throws IOException {

        ServerSocket server = null;
        ExecutorService service = null;
        File file = new File("server/log.txt");

        // Try to open up the listening port
        try {
            server = new ServerSocket(port);
        }
        catch (IOException e) {
            System.err.println("Could not listen on port: "+ port);
            System.exit(-1);
        }
        System.out.println("Server Running");

        // Initialise the executor.
        service = Executors.newFixedThreadPool(10);

        // Clear/Delete log.txt from last execution
        if (file.exists()){
            if(file.delete()){
                System.out.println("Log cleared");
            }else{
                System.out.println("Log couldn't be cleared");
            }
        }else{
            System.out.println("Log empty, nothing to clear");
        }

        // For each new client, submit a new handler to the thread pool.
        while( true )
        {
            Socket client = server.accept();
            service.submit( new ClientHandler(client) );
            System.out.println("Client connected");
        }
    }

}

1 个答案:

答案 0 :(得分:1)

您的"代码字段radius1" 将自己设置为零。

你要么:

  • 永远不会将其设置为默认值以外的任何值,即零,或

  • 你将它设置为非零,然后你有一些你没有向我们展示的其他代码将它设置为零。这是因为它是public,所以任何其他代码都可以修改它。

要修复此问题,请删除public static部分,然后从static函数中删除ShowArea(),并修改所有代码,使其不需要任何静态内容。