从循环中断开

时间:2014-12-09 16:46:48

标签: 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: 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 {
        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();
}

}

3 个答案:

答案 0 :(得分:0)

如果您想在撰写"yes"时继续删除!

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

其次,您应该将整个代码块放在它自己的do-while循环中。

我只是提出这个问题,因为你的第二个do-while循环不会像预期输出那样输出Add or delete another name?

答案 1 :(得分:0)

问题在于:

  ...
  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,因此循环中断。

答案 2 :(得分:0)

对不起,我看得不够好。

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
}