我想在用户在我的程序中退出while循环之前使用一种方法进行仔细检查。使用Java中的方法打破循环
private static Scanner input = new Scanner(System.in);
public static void ays() {
System.out.println("Are you sure?");
String ays = input.nextLine();
if (ays.equals("Yes")) {
break;
} else {
continue;
}
}
在运行的程序,我得到的错误break outside switch or loop
和continue outside switch or loop
。有什么方法可以实现我的目标吗?
'break'只能在循环中使用,因为它打破了循环,并直接导致循环外的第一条语句。 'continue'在循环开始时接受控制,再次丢弃'continue'语句下的所有其他语句。 –
break可用于跳出switch语句http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html或java中的循环http://docs.oracle.com/javase/tutorial /java/nutsandbolts/branch.html 你可以参考张贴的oracle java教程链接。 – AurA