Java赋值中的幻数问题

时间:2019-12-29 23:51:27

标签: java

我正在学校做一个项目,现在我陷入了魔幻数字,希望能得到一些帮助。一切似乎都工作正常,唯一的问题是我的开关盒(情况2,情况3,情况4),因为我收到的错误是正在使用幻数。我尝试了几件事。在声明数字时,似乎没有任何作用。

这是我的代码:

import java.util.Scanner;

public class ProgramSkeleton {

    private static Scanner input = new Scanner(System.in);

    void initialize () {
        System.out.println("Welcome to the dog register!");
        System.out.println("Type 0 to exit");
        System.out.println("Type 1 to register new dog");
        System.out.println("Type 2 to increase age");
        System.out.println("Type 3 to list dogs");
        System.out.println("Type 4 to remove dog");
    }

    int readCommand() {
        System.out.println("> ");
        int command = input.nextInt();
        return command;
    }

    void handleCommand(int command) {
        switch(command) {
        case 0:
            break;
        case 1: 
            System.out.println("you have chosen register new dog");
            break;
        case 2: 
            System.out.println("you have chosen increase age");
            break;
        case 3:
            System.out.println("you have chosen list dogs");
            break;
        case 4:
            System.out.println("you have chosen remove dog");
            break;
        default:
            System.out.println("unknown command!");
        }
    }

    void runCommandLoop() {
        int command;
        do {
        command = readCommand();
        handleCommand(command);
        } while(command != 0);
    }

    void closeDown() {
        System.out.println("Welcome back!");
        input.close();
    }

    void start() {
        initialize();
        runCommandLoop();
        closeDown();
    }

    public static void main(String[] args) {
        ProgramSkeleton program = new ProgramSkeleton();
        program.start();
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码在我的机器上运行得很好,所以我认为该学生服务器一定有些奇怪。

如果您提供更多信息,例如错误消息的文本以及该服务器的名称和期望(如果您以前有过使用经验),可能会有人帮助您。