Java-can';t执行1-7系列中的最后一个if语句
“selection”变量用于调用不同的方法和执行不同的代码块。除了我想退出程序(即选择==7)外,其他一切都正常。有人知道这是为什么吗Java-can';t执行1-7系列中的最后一个if语句,java,if-statement,Java,If Statement,“selection”变量用于调用不同的方法和执行不同的代码块。除了我想退出程序(即选择==7)外,其他一切都正常。有人知道这是为什么吗 public class HW5Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); boolean breakRequest = false; while(breakRequest == false){
public class HW5Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean breakRequest = false;
while(breakRequest == false){
System.out.println("\nWelcome to JVHS, what would you like to do?");
System.out.println("1. Admit a new dog (description unknown)");
System.out.println("2. Admit a new dog (description known)");
System.out.println("3. Display information about a particular dog");
System.out.println("4. Adopt a dog");
System.out.println("5. Euthanize a dog");
System.out.println("6. Display information about the JVHS");
System.out.println("7. Exit");
int selection = scanner.nextInt();
System.out.println("\nYour selection is "+ selection);
HW5Dog dogObject = new HW5Dog();
if(selection == 1)
dogObject.addDog();
if(selection == 2)
dogObject.addDog2();
if(selection == 3){
dogObject.getAllDogs();
//Ask which Dog you want and get its info
System.out.println("Please enter the ID number of the dog you wish to display:");
if(scanner.hasNextInt()){
int dogID = scanner.nextInt();
dogObject.getDog(dogID);
}
else
System.out.println("ERROR - INVALID INPUT: EXPECTING INTERGER, TRY AGAIN");
}
if(selection == 4){
dogObject.getAllDogs();
//Ask which Dog you want and get its info
System.out.println("Please enter the ID number of the dog you wish to adopt:");
if(scanner.hasNextInt()){
int dogID = scanner.nextInt();
dogObject.adopt(dogID);
}
}
if(selection == 5){
dogObject.getAllDogs();
//Ask which Dog you want and get its info
System.out.println("Please enter the ID number of the dog you wish to euthanize:");
if(scanner.hasNextInt()){
int dogID = scanner.nextInt();
dogObject.euth(dogID);
}
if(selection == 6){
dogObject.getJVHS();
}
if(selection == 7){
System.out.println("Goodbye");
scanner.close();
breakRequest = true;
}
}
}
}
}
问题在上面的选择框中:
if(selection == 5){
dogObject.getAllDogs();
//Ask which Dog you want and get its info
System.out.println("Please enter the ID number of the dog you wish to euthanize:");
if(scanner.hasNextInt()){
int dogID = scanner.nextInt();
dogObject.euth(dogID);
}
不关闭第二条if
语句上的大括号。它应该是这样的:
if(selection == 5){
dogObject.getAllDogs();
//Ask which Dog you want and get its info
System.out.println("Please enter the ID number of the dog you wish to euthanize:");
if(scanner.hasNextInt()) {
int dogID = scanner.nextInt();
dogObject.euth(dogID);
}
}
正如一些评论中提到的,您应该坚持使用清晰一致的缩进样式,以便将来更容易发现此问题。问题在上面的选择框中:
if(selection == 5){
dogObject.getAllDogs();
//Ask which Dog you want and get its info
System.out.println("Please enter the ID number of the dog you wish to euthanize:");
if(scanner.hasNextInt()){
int dogID = scanner.nextInt();
dogObject.euth(dogID);
}
不关闭第二条if
语句上的大括号。它应该是这样的:
if(selection == 5){
dogObject.getAllDogs();
//Ask which Dog you want and get its info
System.out.println("Please enter the ID number of the dog you wish to euthanize:");
if(scanner.hasNextInt()) {
int dogID = scanner.nextInt();
dogObject.euth(dogID);
}
}
正如一些评论中提到的,您应该坚持使用清晰一致的缩进样式,以便将来更容易发现此问题。什么不起作用?它怎么不起作用?你期望它做什么?取而代之的是什么呢?看看选择5和下面的{}。正确的缩进是奇迹…什么不起作用?它怎么不起作用?你期望它做什么?取而代之的是什么呢?看看选择5和下面的{}。正确的缩进是奇迹…这就是为什么你总是修正缩进。不仅修正标识,而且在控制语句中使用括号…我不知道你使用哪个IDE,但我建议启用一些文件保存操作,如组织导入和格式化文件,这就是为什么您总是修复缩进。不仅修复标识,而且在控制语句中使用括号…我不知道您使用哪个IDE,但我建议启用一些文件保存操作,如组织导入和格式化文件