简体   繁体   English

Java 无法在 switch 中达到 case 283

[英]Java can't reach case 283 in switch

I had a weird problem with java, while working with Sockets I created a case LOGOUT: (283) for the server to handle (client sends different ints to specify a service request to server), but in my switch the LOGOUT case was unreachable for some reason (IntelliJ told me that this statement was unreachable).我在使用 Java 时遇到了一个奇怪的问题,在使用 Sockets 时,我创建了一个case LOGOUT: (283) 供服务器处理(客户端发送不同的整数来指定对服务器的服务请求),但在我的交换机中,LOGOUT 情况无法访问出于某种原因(IntelliJ 告诉我此语句无法访问)。 So I tried System.out.println(ServiceId) and it was always a different number (not a random one) from 283, it was really weird.所以我尝试了System.out.println(ServiceId)并且它总是与 283 不同的数字(不是随机数字),这真的很奇怪。 I solved this problem by changing the number.我通过更改数字解决了这个问题。 Obviously the number 283 wasn't in no other variable.显然,数字 283 不在其他变量中。 Could it be Java that use a final static int with value 283 for something else?可能是 Java 使用值为 283 的最终静态 int 来做其他事情吗?

PS : Could it be that switch has maximum number of statement where this number is less than 283? PS : 会不会是 switch 有最大语句数,这个数小于 283?

    private static void getService(Socket s) throws IOException {
        InputStream inputStream = s.getInputStream();
        int servId = inputStream.read();
        OutputStream outputStream = s.getOutputStream();
        Runnable task;
        System.out.println("***************** " + servId); // this servID should be the number 283 but it's not.
        switch(servId) {
            case REPLY_EMAIL:
            case FORWARD_EMAIL:
            case REPLY_ALL_EMAIL:
            case WRITE_EMAIL:
                outputStream.write(SUCCESS_RESPONSE);
                task = new EmailReceiver(s,lock, servId, serverLog);
                threadPool.execute(task);
                break;

            case LOGOUT:
                outputStream.write(SUCCESS_RESPONSE);
                threadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String user = Utils.getUser(s);
                            serverLog.writeLog("L'utente " + user + " si è disconnesso.");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;

            case READ_ALL_EMAILS:
                outputStream.write(SUCCESS_RESPONSE);
                task = new EmailReader(s, serverLog);
                threadPool.execute(task);
                break;

            case USER_LIST:
                outputStream.write(SUCCESS_RESPONSE);
                task = new UserGetter(s, serverLog);
                threadPool.execute(task);
                break;

            case LOGIN:
                outputStream.write(SUCCESS_RESPONSE);
                task = new UserLogin(s, serverLog);
                threadPool.execute(task);
                break;

            case DELETE_EMAIL:
                outputStream.write(SUCCESS_RESPONSE);
                task = new EmailDelete(s, serverLog);
                threadPool.execute(task);
                break;

            case NOTIFICATION:
                outputStream.write(SUCCESS_RESPONSE);
                task = new Notificator(s, serverLog);
                threadPool.execute(task);
                break;

            default:
                serverLog.writeLog("Errore di Richiesta");
                outputStream.write(FAILURE_RESPONSE);
                break;
        }
    }

package common;

public class ServiceID {

    public static final int WRITE_EMAIL = 84;
    public static final int READ_ALL_EMAILS = 50;
    public static final int FORWARD_EMAIL = 94;
    public static final int REPLY_EMAIL = 95;
    public static final int REPLY_ALL_EMAIL = 96;
    public static final int DELETE_EMAIL = 100;
    public static final int LOGIN = 11;
    public static final int USER_LIST = 111;
    public static final int SUCCESS_RESPONSE = 1;
    public static final int FAILURE_RESPONSE = -1;
    public static final int NOTIFICATION = 118;
    public static final int EMAIL_NOT_FOUND = 404;
    public static final int LOGOUT = 283;
}

these 2 pieces of code are the functions where I get the ServiceID from InputStream.这两段代码是我从 InputStream 获取 ServiceID 的函数。 The other class is all the possible serviceID number.另一类是所有可能的 serviceID 号。 Don't write : " you are probably pass serviceID wrongly", because this is not the case, all other ServiceID working properly.不要写:“您可能错误地传递了 serviceID”,因为事实并非如此,所有其他 ServiceID 都正常工作。

If InputStream is java.io.InputStream , the return value is an 8-bit value (or -1), so it will not match 283.如果 InputStream 是java.io.InputStream ,则返回值是 8 位值(或 -1),因此不会匹配 283。

public abstract int read() throws IOException public abstract int read() 抛出 IOException

Reads the next byte of data from the input stream.从输入流中读取下一个字节的数据。 The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.值字节以 0 到 255 范围内的 int 形式返回。如果由于已到达流末尾而没有可用字节,则返回值 -1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM