小编Hod*_*hod的帖子

我的switch语句出了什么问题?

我正在尝试在java中创建一个switch语句,但即使我的语法正确Syntax error on token "{", SwitchLabels expected after this token,我也会收到此错误:我知道我可以使用语句,但我的老师告诉我使用switch,因为它看起来更漂亮,所以我要去使用开关.我试图移动input=scan.next()但是这给了我另一个错误

switch (true) {
    input = scan.next();
    case 1:
        input.equals("T");
        outToServer.writeBytes("T\r\n");
        System.out.println(inFromServer.readLine());
        break;

    case 2:
        input.equals("S");
        outToServer.writeBytes("S\r\n");
        System.out.println(inFromServer.readLine());
        break;

    case 3:
        input.equals("Z");
        outToServer.writeBytes("Z\r\n");
        System.out.println(inFromServer.readLine());
        break;

    case 4:
        input.equals("D");
        System.out.println("Write a message");
        text = scan.next();
        outToServer.writeBytes("D " + text + "\r\n");
        System.out.println(inFromServer.readLine());
        break;

    case 5:
        input.equals("DW");
        outToServer.writeBytes("DW\r\n");
        System.out.println(inFromServer.readLine());
        break;

    case 6:
        input.equals("RM20");
        text = "RM20 4" + "\"" + text1 + "\" \"" + text2 + "\" \"" …
Run Code Online (Sandbox Code Playgroud)

java switch-statement

-1
推荐指数
1
解决办法
379
查看次数

标签 统计

java ×1

switch-statement ×1