import java.util.Scanner;
public class EmpTest {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// there is a second class with my constructors and other stuff of that nature
Employee e1 = new Employee();
Employee e2 = new Employee();
Employee e3 = new Employee();
String no = "no";
String yes = "yes";
while (true) {
System.out.println("Please enter in the employee name");
e1.name = input.nextLine();
System.out.println("Please enter in the salary of that employee");
e1.salary = input.nextDouble();
while (true) {
System.out.println("Would you like to add a second employee?");
String userInput = input.next();
if (userInput.equalsIgnoreCase(yes)) {
System.out.println("Please enter in the employee name");
e2.name = input.next();
System.out.println("Please enter in the salary of that employee");
e2.salary = input.nextDouble();
}
//this is where my break wont terminate the loop
//the console output just asks for the second employee salary and name without waiting for a user input
if(userInput.equalsIgnoreCase(no)) {
System.out.println("Okay");
break;
}
System.out.println("Would you like to add a third employee?");
userInput = input.next();
if (userInput.equalsIgnoreCase(yes)) {
System.out.println("Please enter in the employee name");
e3.name = input.next();
System.out.println("Please enter in the salary of that employee");
e3.salary = input.nextDouble();
}
if(userInput.equalsIgnoreCase(no)) {
System.out.println("Okay");
break;
}
}
}
}
}
答案 0 :(得分:0)
在我的第一个错误答案之后,这是正确的。正如JLS:
没有标签的break语句尝试将控制转移到最里面 封闭开关,同时,做,或立即封闭的声明 方法
所以在你的情况下,你是break
内部while(true)
- 循环。
如果您想要break
某些“外部”区块,则必须使用break
这样的标签:
loop:
while(true) {
.....
while(true) {
...
break loop;
}
}
答案 1 :(得分:0)
你的break语句在while循环中打破内部但是没有break语句用于外部while循环。
答案 2 :(得分:0)
你必须提供2个break语句,一个用于内部while
循环,另一个用于外部while
循环。
答案 3 :(得分:0)
以下是对代码的一些解释的答案:
import java.util.Scanner;
public class EmpTest {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
Employee e1 = new Employee();
Employee e2 = new Employee();
Employee e3 = new Employee();
String no = "no";
String yes = "yes";
//initialized userInput here so that we can use it on the outer loop
String userInput="";
while (true) {
System.out.println("Please enter in the employee name");
e1.name = input.nextLine();
System.out.println("Please enter in the salary of that employee");
e1.salary = input.nextDouble();
input.nextLine();
//nextDouble() reads only the double and doesn't finish the line, so we need to add nextLine() method after nextDouble()
while (true) {
System.out.println("Would you like to add a second employee?");
userInput = input.nextLine();
if (userInput.equalsIgnoreCase(yes)) {
System.out.println("Please enter in the employee name");
e2.name = input.nextLine();
System.out.println("Please enter in the salary of that employee");
e2.salary = input.nextDouble();
//again
input.nextLine();
}
if(userInput.equalsIgnoreCase(no)) {
System.out.println("Okay");
break;
}
System.out.println("Would you like to add a third employee?");
userInput = input.nextLine();
if (userInput.equalsIgnoreCase(yes)) {
System.out.println("Please enter in the employee name");
e3.name = input.nextLine();
System.out.println("Please enter in the salary of that employee");
e3.salary = input.nextDouble();
input.nextLine();
}
if(userInput.equalsIgnoreCase(no)) {
System.out.println("Okay");
break;
}
}
//do the remaining things
//add the condition for breaking the outer loop if there's any
break;
}
//never forget to close the scanner
input.close();
}
}
一些注意事项:
nextDouble()读取只有1次而未完成该行,所以我 添加了input.nextLine()来完成该行。
我刚在内循环后面的外循环上添加了一个中断。 我不知道你是否有打破外环的条件,或者 如果在打破内心之后你想做什么的话 环
您也可以将该循环转换为方法,只需使用“return” 声明要突破这个方法。我不知道是否允许 但是,我只是坚持你如何创建程序并进行修改 它有点。
- 醇>
使用后永远不要忘记关闭扫描仪