while while循环满足2个条件之一

时间:2015-04-14 15:03:39

标签: java while-loop

我尝试使用do while循环来确定用户是否想要将狗或猫检入Java中的狗窝系统。这个想法是他们进入了#34; dog"或者" cat"出现提示时,任何条目都将导致错误,并再次提示他们输入文件名。

如果" cat"或"狗"已输入,然后将等效文件分配给程序(dogs.txt或cats.txt),然后系统将运行并将该数据加载到程序中。

以下是当前变量:

private String filename; // holds the name of the file
private Kennel kennel; // holds the kennel
private Scanner scan; // so we can read from keyboard
private String tempFileName;
private String dogsFile = "dogs.txt";
private String catsFile = "cats.txt";

以及导致问题的方法:

private KennelDemo() {
    scan = new Scanner(System.in);

    boolean fileNotCorrect = false;

    System.out.print("Which animal are you looking to check into the kennel?: " + "\n");
    System.out.println("Dog");
    System.out.println("Cat");  
    tempFileName = scan.next();

    do {
        tempFileName.equals("dog");
        filename = dogsFile;
        fileNotCorrect = true;

        /*tempFileName.equals("cat");
        filename = catsFile;
        fileNotCorrect = true;*/
    }
        while(fileNotCorrect = false);
        System.out.println("That is not a valid filename, please enter either 'dog' or 'cat' in lowercase.");

以下是运行代码时打印的内容:

**********HELLO***********
Which animal are you looking to check into the kennel?: 
Dog
Cat
cat
That is not a valid filename, please enter either 'dog' or 'cat' in lowercase.
Using file dogs.txt

无论输入的是什么,它都会为程序分配文件,然后继续加载程序。

我尝试过使用catch {但由于某些原因它无法正常工作,有人可以提供任何帮助吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

这不是怎么做的。你甚至没有检查。

使用此:

 do {
    System.out.print("Which animal are you looking to check into the kennel?: " + "\n");
    System.out.println("Dog");
    System.out.println("Cat");  
    tempFileName = scan.next();
    if(tempFileName.equals("dog") || tempFileName.equals("cat"))
    { 
       filename = tempFileName.equals("dog") ? dogsFile : catsFile;
       fileNotCorrect = true;
    }else{
      System.out.println("That is not a valid filename, please enter either 'dog' or 'cat' in lowercase.");
    }
}
    while(!fileNotCorrect);

答案 1 :(得分:1)

正如其他答案暗示的那样,但是逻辑错误,你错误地检查了你的do-while循环。

以下是您的代码的正确摘要......

Private KennelDemo() {
scan = new Scanner(System.in);

boolean fileNotCorrect = false;
do{
    System.out.print("Which animal are you looking to check into the kennel?: " + "\n");
    System.out.println("Dog");
    System.out.println("Cat");  
    tempFileName = scan.next();
    if (tempFileName.equalsIgnoreCase("dog") || tempFileName.equalsIgnoreCase("cat")){
        //we want to set to true so we break out of the loop
        fileNotCorrect = true;
    } else{
         System.out.println("That is not a valid filename, please enter either 'dog' or 'cat' in lowercase.");
    }
}while(!fileNotCorrect);//do this while fileNotCorrect == false;


    if(tempFileName.equals("dog")){
        filename = dogsFile;
    }
    else if (tempFileName.equals("cat")){
        filename = catsFile;
    }

由于你在do-while循环上声明你的fileNotCorrec为false ...你甚至不需要一个do-while ...一个简单的while循环对你有用

while(!fileNotCorrect){
   ....
}

请阅读https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

这对于一些逻辑阅读而言 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html