【发布时间】:2021-06-15 09:37:25
【问题描述】:
我想更新我的变量,我的代码有一个错误,我不知道哪里有错误
String name = " ";
String family = " ";
int age = 0;
Scanner input = new Scanner(System.in);
int choise;
while (true){
System.out.println("1:Name 2:Family 3:Age ---- 0:Exit");
choise = input.nextInt();
if (choise == 0) break;
else if (choise == 1){
System.out.println("Please enter the name : ");
name = input.nextLine();
}
else if (choise == 2){
System.out.println("Please enter the family : ");
family = input.nextLine();
}
else if (choise == 3){
System.out.println("Please enter the age : ");
}
}
【问题讨论】:
-
错误是什么?
-
"我不知道哪里有错误" - 我们也不知道,因为我们不知道您的代码应该做什么,即您期望什么,并且它正在做什么。
-
您到底想做什么?预期的行为是什么?
-
错误是:在 java.util.Scanner.throwFor(Scanner.java:864) 在 java.util.Scanner.next(Scanner.java) 的线程 "main" java.util.InputMismatchException 中的异常:1485) 在 java.util.Scanner.nextInt(Scanner.java:2117) 在 java.util.Scanner.nextInt(Scanner.java:2076) 在 Code_4.main(Code_4.java:14) @tomerpacific