我的'Do'循环我做错了什么?

时间:2014-01-16 12:29:23

标签: java loops

这里的Java新手,               我试着编写一台oracle机器并且它工作正常。但是,询问用户是否要继续的最后一部分是不起作用的。我会询问用户是否要继续,如果他们没有回复'Y','y','N'或'n',他们将不得不再次重新运行测试。 所以我得到的问题是:

    What is the question that you want to ask?... agafds
    No! And Stop asking me!
    Continue asking?(Y/N)...Y
    Continue asking?(Y/N)...Y
    Continue asking?(Y/N)...Y
    Continue asking?(Y/N)...Y
    Continue asking?(Y/N)...Y

由此:

import java.util.Scanner;
import java.util.Random;
import static java.lang.System.out;

public class firstClass {
public static void main(String args[]) {


    Scanner kbScanner=new Scanner(System.in);
    Random intRand=new Random();
    String yOn;

    do {
        int oneten;
        out.print("What is the question that you want to ask?... ");
        kbScanner.nextLine();
        oneten=intRand.nextInt(10)+1;

        switch (oneten) {
        case 1 :
            out.println("Not a chance, buster.");
            break;
        case 2 :
            out.println("Get a life man!");
            break;
        case 3 :
            out.println("Why are you even asking me this?!");
            break;
        case 4 :
            out.println("Depends on your thinking.");
            break;
        case 5 :
            out.println("What are you seeking for?");
            break;
        case 6 :
            out.println("Maybe... But don't trust me...");
            break;
        case 7 :
            out.println("Do I look like I care?");
            break;
        case 8 :
            out.println("Why would you even ask that?");
            break;
        case 9 :
            out.println("The chances are as thin as searching for a needle in a haystack.");
            break;
        case 10 :
            out.println("No! And Stop asking me!");
            break;
        }
        do{
            out.print("Continue asking?(Y/N)...");
            yOn=kbScanner.nextLine();
        }while (!yOn.equals("Y")||!yOn.equals("y")||!yOn.equals("N")||!yOn.equals("n"));
        out.println("Continuing");
    }while (yOn.equals("Y")||yOn.equals("y"));

}
}

所以:问题在于,无论用户输入什么,似乎都没有区别。

6 个答案:

答案 0 :(得分:3)

!yOn.equals("Y")||!yOn.equals("y")||!yOn.equals("N")||!yOn.equals("n")

这个条件总是要评估为真,如果它是Y而不是N,那么它仍然是真的,反之亦然。将您的||更改为&&

!yOn.equals("Y")&&!yOn.equals("y")&&!yOn.equals("N")&&!yOn.equals("n")

答案 1 :(得分:1)

在你的情况下,内循环应该是这样的我猜:

do{
    out.print("Continue asking?(Y/N)...");
    yOn=kbScanner.nextLine();
}while (!yOn.equals("Y")&&!yOn.equals("y")&&!yOn.equals("N")&&!yOn.equals("n"));

答案 2 :(得分:0)

在switch-case之后,没有另一个do..while循环。仅保留外循环:

//do{
        out.print("Continue asking?(Y/N)...");
        yOn=kbScanner.nextLine();
   // }while (!yOn.equals("Y")||!yOn.equals("y")||!yOn.equals("N")||!yOn.equals("n"));
  //  out.println("Continuing");
//Retain the outer do..while condition
}while (yOn.equals("Y")||yOn.equals("y"));

答案 3 :(得分:0)

尝试通过

while (!(yOn.equals("Y")||yOn.equals("y")||yOn.equals("N")||yOn.equals("n")));

替换第一个while循环

答案 4 :(得分:0)

所以给你一些提示

1 - 使用equalsIgnoreCase代替equals("Y")||!yOn.equals("y") 2 - 删除你应该像跟随

那样的中间while
main {    
   read option

    do {
      process 
      read option 
    } while (continue ?) // validate here 
}

答案 5 :(得分:0)

尝试替换

do{
            out.print("Continue asking?(Y/N)...");
            yOn=kbScanner.nextLine();
        }while (!yOn.equals("Y")&&!yOn.equals("y")&&!yOn.equals("N")&&!yOn.equals("n"));
       out.println("Continuing");

通过

out.print("Continue asking?(Y/N)...");
yOn=kbScanner.nextLine();

你真的不需要内部的do-while循环,因为外部循环可以为你做同样的事情。

尝试thisthis,看看嵌套循环何时真正有用。