从循环中断

首先,我已经validation了我的代码,所以它一直重复,直到被问到“继续?”时给出“是”或“否”。 但是我的代码在输入随机值后从循环中断开然后是。 例如:

Add or delete another name? Add Please enter a name you want to add: Matt Continue? f Continue? yes 

它应该说:

 Add or delete another name? Add Please enter a name you want to add: Matt Continue? f Continue? yes Add or delete another name? 

实际代码

 import java.io.File; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; public class AddOrDeleteNames { public static void main(String[] args) throws Exception { ArrayList names = new ArrayList(); Scanner scan = new Scanner(new File("names.txt")); Scanner myScan = new Scanner(System.in); Scanner scanRedo = new Scanner(System.in); String userRedo; String userResponse; while (scan.hasNext()) names.add(scan.next()); do { System.out.print("Add or delete another name? "); userResponse = myScan.next(); if (userResponse.equalsIgnoreCase("add")) { System.out.print("Please enter a name you want to add: "); names.add(myScan.next()); } else if (userResponse.equalsIgnoreCase("delete")) { System.out.print("Please enter a name you want to delete: "); names.remove(myScan.next()); } else { System.out.print("Invalid Choice"); } PrintWriter writer = new PrintWriter("namesupdated.txt"); for (int i = 0; i < names.size(); i++) writer.println(names.get(i)); writer.close(); System.out.print("Continue? "); userRedo = scanRedo.next(); } while (userRedo.equalsIgnoreCase("yes")); do { if(userRedo.equalsIgnoreCase("no")) { System.out.print("Thank You."); userRedo = scanRedo.next(); } else if (!userRedo.equalsIgnoreCase("yes")) { System.out.print("Continue? "); // LOOP ENDS EARLY : FIX! userRedo = scanRedo.next(); } } while (!userRedo.equalsIgnoreCase("yes")); scan.close(); myScan.close(); scanRedo.close(); } 

}

如果你想继续当你写"yes"摆脱了!

 while (!userRedo.equalsIgnoreCase("yes")); 

其次,你应该把你想要继续的整个代码块放在它自己的do-while循环中。

我只是提起这个,因为你的第二个do-while循环不会输出Add or delete another name? 像你的预期输出会建议。

问题出在这里:

  ... else if (!userRedo.equalsIgnoreCase("yes")) { System.out.print("Continue? "); // LOOP ENDS EARLY : FIX! userRedo = scanRedo.next(); } } while (!userRedo.equalsIgnoreCase("yes")); 

当你回答“f”时,它会进入else-if子句。

然后它接受“是”并覆盖userRedo。 因此,while条件返回false,因此循环中断。

对不起,我看得不够好。

Java从上到下,从左到右读取内容。 你做的是在主要部分之后部署另一个do...while循环。 所以,在你说“是”继续第二次之后do...while循环停止执行并关闭所有扫描仪。

 do { if(userRedo.equalsIgnoreCase("no")) { System.out.print("Thank You."); userRedo = scanRedo.next(); } else if (!userRedo.equalsIgnoreCase("yes")) { System.out.print("Continue? "); // LOOP ENDS EARLY : FIX! userRedo = scanRedo.next(); } } while (!userRedo.equalsIgnoreCase("yes")); scan.close(); myScan.close(); scanRedo.close(); 

}

之后你就关闭了你的课程,程序假定它没有什么可做的。 如果你调用一个单独的方法,Java只会在其读取中“重新开始”。 所以,为了让它恢复并让你保持你的do...while循环,你只需要将它们放在不同的方法中,在给出批准的答案时调用它们。

例如:

 //Declare variables up here, it will make the entire class have access to them public static void main(String[] args){ loop(); } public static void loop(){ //do...while here System.out.println("Continue?"); check(); } public static void check(){ //Scanner.in() //do..while #2 here //User enters yes then: loop(); //User enters no then: //whatever you want here }