程序中没有错误。 PASS、CLEARED 用户输入正在按预期工作。 但是,当我在嵌套 if 中输入 NO 作为用户输入时,控件会正确选择“else 语句”,但它也会进入内部,并执行下一个 S.O.P 显示,例如“你通过了第二级面试吗?”如果一级面试未通过,则不需要。因此,它显示 else 和 next if 相关语句。但我只想显示 else 语句并退出。
package controlStatements;
import java.util.Scanner;
class nested_if_else_Statement {
static public void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Online Assesment, enter PASS or FAIL?");
String oa = scan.nextLine();
if (oa.equals("PASS")) {
System.out.println("Please wait in the office lobby for further levels of interview.");
System.out.println("Have you CLEARED first level of interview?");
String level1 = scan.nextLine();
if(level1.equals("CLEARED")) {
System.out.println("Please wait for second level of interview.");
}else {
System.out.println("We're Sorry, better luck next time.");
}
System.out.println("Have you CLEARED second level of interview?");
String level2 = scan.nextLine();
if(level2.equals("CLEARED")) {
System.out.println("Please wait for third level of interview.");
}else {
System.out.println("We're sorry, better luck next time.");
}
System.out.println("Have you CLEARED third level of interview?");
String level3 = scan.nextLine();
if(level3.equals("CLEARED")) {
System.out.println("Please wait for fourth level of interview.");
}else {
System.out.println("We're sorry, better luck next time.");
}
System.out.println("Have you CLEARED fourth level of interview?");
String level4 = scan.nextLine();
if(level4.equals("CLEARED")) {
System.out.println("Kindly wait for final level of HR Interview.");
System.out.println("Thank you for your patience.");
}else {
System.out.println("We're sorry, better luck next time.");
}
System.out.println("Have you DONE with your HR Interview?");
String levelHR = scan.nextLine();
if(levelHR.equals("DONE")) {
System.out.println("Please wait in the office lobby, will get back shortly.");
}
} else {
System.out.println("We're Sorry, better luck next time");
}
scan.close();
}
}
这些声明正在按预期发挥作用。它捕获了
else
,但您从未告诉它 stop 运行,因此它继续执行下一步。解决此问题的一个简单方法是每当您想要退出代码时添加 return
语句。
if (/* whatever */) {
// do stuff
} else {
// do stuff
return;
}
// other stuff that won't be ran if `return` is reached
您需要使用
return
将控制流直接引出函数。这将阻止控制流评估下一个条件。
class nested_if_else_Statement {
static public void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Online Assesment, enter PASS or FAIL?");
String oa = scan.nextLine();
if (oa.equals("PASS")) {
System.out.println("Please wait in the office lobby for further levels of interview.");
System.out.println("Have you CLEARED first level of interview?");
String level1 = scan.nextLine();
if (level1.equals("CLEARED")) {
System.out.println("Please wait for second level of interview.");
} else {
System.out.println("We're Sorry, better luck next time.");
return;
}
System.out.println("Have you CLEARED second level of interview?");
String level2 = scan.nextLine();
if (level2.equals("CLEARED")) {
System.out.println("Please wait for third level of interview.");
} else {
System.out.println("We're sorry, better luck next time.");
return;
}
// ...
作为附加建议,您可以通过测试
oa.equals("PASS")
而非 !oa.equals("PASS")
来删除一定程度的缩进。通过在这种情况下尽早退出,我们可以消除一定程度的缩进。
package controlStatements;
import java.util.Scanner;
class nested_if_else_Statement {
static public void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Online Assesment, enter PASS or FAIL?");
String oa = scan.nextLine();
if (!oa.equals("PASS")) {
System.out.println("We're Sorry, better luck next time");
scan.close();
return;
}
System.out.println("Please wait in the office lobby for further levels of interview.");
System.out.println("Have you CLEARED first level of interview?");
String level1 = scan.nextLine();
if (level1.equals("CLEARED")) {
System.out.println("Please wait for second level of interview.");
} else {
System.out.println("We're Sorry, better luck next time.");
return;
}
System.out.println("Have you CLEARED second level of interview?");
String level2 = scan.nextLine();
if (level2.equals("CLEARED")) {
System.out.println("Please wait for third level of interview.");
} else {
System.out.println("We're sorry, better luck next time.");
return;
}
System.out.println("Have you CLEARED third level of interview?");
String level3 = scan.nextLine();
if (level3.equals("CLEARED")) {
System.out.println("Please wait for fourth level of interview.");
} else {
System.out.println("We're sorry, better luck next time.");
return;
}
System.out.println("Have you CLEARED fourth level of interview?");
String level4 = scan.nextLine();
if (level4.equals("CLEARED")) {
System.out.println("Kindly wait for final level of HR Interview.");
System.out.println("Thank you for your patience.");
} else {
System.out.println("We're sorry, better luck next time.");
return;
}
System.out.println("Have you DONE with your HR Interview?");
String levelHR = scan.nextLine();
if (levelHR.equals("DONE")) {
System.out.println("Please wait in the office lobby, will get back shortly.");
}
scan.close();
}
}