Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要帮助使循环工作吗_Java - Fatal编程技术网

Java 需要帮助使循环工作吗

Java 需要帮助使循环工作吗,java,Java,我已经循环了我的代码,所以它会不断重复,直到被问到“继续”时给出“是”或“否”?。但是我的代码在输入一个随机值后从循环中断,然后是。 例如: 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: Mat

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

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<String> names = new ArrayList<String>();
    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 { // THIS LOOP IS HERE BECAUSE IF THE USER ENTERS A VALUE OTHER THAN CONTINUE, YES OR NO, THE QUESTION REPEATS
        if(userRedo.equalsIgnoreCase("no")) {
            System.out.print("Thank You.");
            userRedo = scanRedo.next();
        } else if (!userRedo.equalsIgnoreCase("yes")) {
            System.out.print("Continue? ");  // LOOP ENDS EARLY HERE
            userRedo = scanRedo.next();  
        }
    } while (!userRedo.equalsIgnoreCase("yes")); // NOT SURE HOW TO RESTART PREVIOUS LOOP

    scan.close();
    myScan.close();
    scanRedo.close();
}
}

执行此操作的方法始终是使用带有某种可更改条件的while循环:

Scanner scan = new Scanner(System.in);

boolean stop = false; 
while(!stop) {
    //do whatever

    ...

    System.out.println("Continue? Yes or No");
    String s = Scan.nextLine();
    if(s.equals("No")) {
        stop = true;
    }
}

非常聪明。。用答案销毁旧的,用完全相同的代码创建一个新的。我修改了以前的代码,因为它是错误的。除了最后一部分中的注释,我看不到任何区别。此外,您应该更新而不是销毁旧问题,而不是创建新问题。很抱歉,这不起作用