初学者:Java While循环不会退出?

时间:2015-04-20 21:19:59

标签: java while-loop

public static void main(String[] args) {
    StackX theStack = new StackX(99);

    String current = "Enterprise";
    int logOut = 1;
    String planetName;
    int secretCode;
    //This is the start out, do not loop this
    System.out.println("You are on the enterprise, where would you like to teleport?");

    //while loop will be done after reaching Io
    do {

        Scanner input = new Scanner(System.in);
        //Grab input from user
        System.out.println("Enter a planet name: ");
        planetName = input.nextLine();
        System.out.println("Enter a the four digit code for " + planetName + ": ");
        secretCode = input.nextInt();

        if (current.equals("Enterprise")){

            if (planetName.equalsIgnoreCase("Europa") && secretCode == 9007){
                current = planetName;
                Moon Europa = new Moon(planetName, secretCode);
                theStack.push(Europa);
            } else if (planetName.equalsIgnoreCase("Titan") && secretCode == 1232){
                current = planetName;
                Moon Titan = new Moon(planetName, secretCode);
                theStack.push(Titan);
            } else if (planetName.equalsIgnoreCase("Rhea") && secretCode == 5623){
                current = planetName;
                Moon Rhea = new Moon(planetName, secretCode);
                theStack.push(Rhea);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Titan")){

            if (planetName.equalsIgnoreCase("Enterprise") && secretCode == 1212){
                current = planetName;
                Moon Enterprise = new Moon(planetName, secretCode);
                theStack.push(Enterprise);
            } else if (planetName.equalsIgnoreCase("Rhea") && secretCode == 5623){
                current = planetName;
                Moon Rhea = new Moon(planetName, secretCode);
                theStack.push(Rhea);
            } else if (planetName.equalsIgnoreCase("Elara") && secretCode == 1264){
                current = planetName;
                Moon Elara = new Moon(planetName, secretCode);
                theStack.push(Elara);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Rhea")){

            if (planetName.equalsIgnoreCase("Enterprise") && secretCode == 1212){
                current = planetName;
                Moon Enterprise = new Moon(planetName, secretCode);
                theStack.push(Enterprise);
            } else if (planetName.equalsIgnoreCase("Titan") && secretCode == 1232){
                current = planetName;
                Moon Titan = new Moon(planetName, secretCode);
                theStack.push(Titan);
            } else if (planetName.equalsIgnoreCase("Europa") && secretCode == 9007){
                current = planetName;
                Moon Europa = new Moon(planetName, secretCode);
                theStack.push(Europa);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Europa")){

            if (planetName.equalsIgnoreCase("Enterprise") && secretCode == 1212){
                current = planetName;
                Moon Enterprise = new Moon(planetName, secretCode);
                theStack.push(Enterprise);
            } else if (planetName.equalsIgnoreCase("Rhea") && secretCode == 5623){
                current = planetName;
                Moon Rhea = new Moon(planetName, secretCode);
                theStack.push(Rhea);
            } else if (planetName.equalsIgnoreCase("Metis") && secretCode == 2535){
                current = planetName;
                Moon Metis = new Moon(planetName, secretCode);
                theStack.push(Metis);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Elara")){

            if (planetName.equalsIgnoreCase("Titan") && secretCode == 1232){
                current = planetName;
                Moon Titan = new Moon(planetName, secretCode);
                theStack.push(Titan);
            } else if (planetName.equalsIgnoreCase("Metis") && secretCode == 2535){
                current = planetName;
                Moon Metis = new Moon(planetName, secretCode);
                theStack.push(Metis);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Metis")){

            if (planetName.equalsIgnoreCase("Europa") && secretCode == 9007){
                current = planetName;
                Moon Europa = new Moon(planetName, secretCode);
                theStack.push(Europa);
            } else if (planetName.equalsIgnoreCase("Elara") && secretCode == 1264){
                current = planetName;
                Moon Elara = new Moon(planetName, secretCode);
                theStack.push(Elara);
            } else if (planetName.equalsIgnoreCase("Io") && secretCode == 4792){
                current = planetName;
                Moon Io = new Moon(planetName, secretCode);
                theStack.push(Io);
                System.out.println("You have acquired the Macho Orb! Return to the Enterprise.");
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Io")){

            if (planetName.equalsIgnoreCase("Metis") && secretCode == 2535){
                current = planetName;
                Moon Metis = new Moon(planetName, secretCode);
                theStack.push(Metis);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else {
            logOut = 0;
            System.out.println("You are dead.");

        }

    } while((!current.equals("Io")) || (logOut != 0));


}

这里的主要问题是do-while循环不会退出。该计划的前提是从行星跳到行星,而行星名称是目的地,当前行星是您当前行星。我想要的是当电流为“Io”时退出的循环。每次“跳”到另一个星球后,目的地成为当前行星。我测试了它,当它应该在

时,当前确实等于“Io”
    if (planetName.equalsIgnoreCase("Io") && secretCode == 4792) {
        current = planetName;
        Moon Io = new Moon(planetName, secretCode);
        theStack.push(Io);
        System.out.println("You have acquired the Macho Orb! Return to the Enterprise.");
    }

然而,循环继续。

1 个答案:

答案 0 :(得分:2)

while((!current.equals(" Io"))||(logOut!= 0));

应改为

while((!current.equals(" Io"))&&(logOut!= 0));