簡體   English   中英

Java 無法在 switch 中達到 case 283

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

我在使用 Java 時遇到了一個奇怪的問題,在使用 Sockets 時,我創建了一個case LOGOUT: (283) 供服務器處理(客戶端發送不同的整數來指定對服務器的服務請求),但在我的交換機中,LOGOUT 情況無法訪問出於某種原因(IntelliJ 告訴我此語句無法訪問)。 所以我嘗試了System.out.println(ServiceId)並且它總是與 283 不同的數字(不是隨機數字),這真的很奇怪。 我通過更改數字解決了這個問題。 顯然,數字 283 不在其他變量中。 可能是 Java 使用值為 283 的最終靜態 int 來做其他事情嗎?

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;
}

這兩段代碼是我從 InputStream 獲取 ServiceID 的函數。 另一類是所有可能的 serviceID 號。 不要寫:“您可能錯誤地傳遞了 serviceID”,因為事實並非如此,所有其他 ServiceID 都正常工作。

如果 InputStream 是java.io.InputStream ,則返回值是 8 位值(或 -1),因此不會匹配 283。

public abstract int read() 拋出 IOException

從輸入流中讀取下一個字節的數據。 值字節以 0 到 255 范圍內的 int 形式返回。如果由於已到達流末尾而沒有可用字節,則返回值 -1。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM