我正在学校做一个项目,我现在陷入了神奇的数字,希望得到一些帮助。一切似乎都工作正常,唯一的问题是我的开关案例(案例 2、案例 3、案例 4),因为我收到的错误是正在使用魔数(Magic Number)。我尝试了几件事。正如宣布数字但似乎没有任何作用。
这是我的代码:
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();
}
}
最佳答案
你的代码在我的机器上运行得很好,所以我认为那个学生服务器一定有什么奇怪的地方。
如果您提供更多信息,例如错误消息的文本以及该服务器是什么以及它期望什么(如果您以前有过使用它的经验),有人可能会为您提供帮助。
关于java - java赋值中的魔数(Magic Number)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59524328/