繁体   English   中英

如何在Java中退出嵌套的while循环?

[英]How do I exit a nested while loop in java?

编辑:由于我的程序仍然无法正常工作,我发布了整个方法,以防万一还有其他问题。

如果用户在提示时输入“ n”,我想退出此程序:

 char again = 'y';
        while (again == 'y' || again == 'Y')
        { 
        String ans = IBIO.inputString ("Unscramble: OBORSLW (Hint: Shellder latches onto its tail.) ");
        int tries = 0;

        while (!ans.toLowerCase ().equals ("slowbro"))
        {
            System.out.println ("Incorrect. Wrong answer. Try again.");
            tries++;
            ans = IBIO.inputString ("\nUnscramble: OBORSLW (Hint: Shellder latches onto its tail.) ");

            if (tries > 3)
            {
                System.out.println ("The correct answer was SLOWBRO.");
                again = IBIO.inputChar ("Play again? (y/n) ");
                break;
            }
        }

        System.out.println ("Correct.");

        System.out.println ("\nQuestion #2 - ");
        String ans2 = IBIO.inputString ("\nUnscramble: RVLEGERA (Hint: It rolls down slopes without slowing down.) ");
        int tries2 = 0;
        while (!ans2.toLowerCase ().equals ("graveler"))
        {
            System.out.println ("Incorrect. Wrong answer. Try again.");
            tries2++;
            ans2 = IBIO.inputString ("\nUnscramble: RVLEGERA (Hint: It rolls down slopes without slowing down.) ");

            if (tries2 > 3)
            {
                System.out.println ("The correct answer was GRAVELER.");
                again = IBIO.inputChar ("Play again? (y/n) ");

                if (again != 'y' || again != 'Y')
                    break;
            }
        }
        System.out.println ("Correct.");

        System.out.println ("\nQuestion #3 -");
        String ans3 = IBIO.inputString ("\nUnscramble: TYSGLA (Hint: It's almost invisible and is gaseous.) ");
        int tries3 = 0;
        while (!ans3.toLowerCase ().equals ("gastly"))
        {
            System.out.println ("Incorrect. Wrong answer. Try again.");
            tries3++;
            ans3 = IBIO.inputString ("\nUnscramble: TYSGLA (Hint: It's almost invisible and is gaseous.) ");

            if (tries3 > 3)
            {
                System.out.println ("The correct answer was GASTLY.");
                again = IBIO.inputChar ("Play again? (y/n) ");

                if (again != 'y' || again != 'Y')
                    break;
            }
        }
        printSlow ("Correct.");
        printSlow ("\nWell Done, " +name+ "! You have passed the test! I'm so happy for you!!");
        break;
    }
    }

每当我故意多次输入错误值时,都会得到正确答案,如果需要,可以重试。 这部分有效。 但是,如果我不想继续,该程序将自行继续并继续下一个问题。 如何停止整个程序?

 char again = 'y';
    while (again == 'y' || again == 'Y')
    {
        String ans = IBIO.inputString ("Unscramble: OBORSLW (Hint: Shelder latches onto its tail.) ");
        int tries = 0;
        boolean flag= false;
        while (!ans.toLowerCase ().equals ("slowbro"))
        {
            System.out.println ("Incorrect. Wrong answer. Try again.");
            tries++;
            ans = IBIO.inputString ("\nUnscramble: OBORSLW (Hint: Shelder latches onto its tail.) ");

            if (tries > 3)
            {
                System.out.println ("The correct answer was SLOWBRO.");
                again = IBIO.inputChar ("Play again? (y/n) ");
                flag = true;
                break;
            }
        }
        if(flag==true){
        break;       
} 

你这个意思是?

编辑:我有修改代码,以满足您的要求。 请检查一下。

原文:我想首先您必须在这里写下您的IBIO类,以便人们能够帮助您进行调试。

我已经在线找到了IBIO类并测试了您的程序,但是它已正确执行。

下面是我的代码。

Main.java:

public class Main {
    private static boolean verifyAnswer(String candidate, String answer) {
        if (candidate.toLowerCase().equals(answer))
            return true;
        else {
            System.out.println("Incorrect. Wrong answer. Try again.");
            return false;
        }
    }

    private static boolean playGame(int questionNumber, String answer, String question) {
        String candidate;
        System.out.println("Question #" + questionNumber + " - ");
        int tries = 0;
        do {
            tries++;
            if (tries > 3) {
                System.out.println("The correct answer was " + answer + ".");
                char again = IBIO.inputChar("Play again? (y/n) ");
                if (again != 'y' && again != 'Y')
                    System.exit(0);
                else
                    return false;
            }
            candidate = IBIO.inputString(question);
        } while (!verifyAnswer(candidate, answer));
        System.out.println("Correct.");
        return true;
    }

    public static void main(String[] args) {
        while (!playGame(1, "slowbro", "Unscramble: OBORSLW (Hint: Shellder latches onto its tail.) ")) ;
        while (!playGame(2, "graveler", "Unscramble: RVLEGERA (Hint: It rolls down slopes without slowing down.) ")) ;
        while (!playGame(3, "gastly", "Unscramble: TYSGLA (Hint: It's almost invisible and is gaseous.) ")) ;
    }
}

IBIO.java:

//---- IBIO - Below are simplified input and output methods ----
//===========================================================
// IBIO Standard Input and Output
//  These methods must be copied into your program(s).
//===========================================================
public class IBIO {
    static void output(String info) {
        System.out.println(info);
    }

    static void output(char info) {
        System.out.println(info);
    }

    static void output(byte info) {
        System.out.println(info);
    }

    static void output(int info) {
        System.out.println(info);
    }

    static void output(long info) {
        System.out.println(info);
    }

    static void output(double info) {
        System.out.println(info);
    }

    static void output(boolean info) {
        System.out.println(info);
    }

    static String input(String prompt) {
        String inputLine = "";
        System.out.print(prompt);
        try {
            inputLine = (new java.io.BufferedReader(
                    new java.io.InputStreamReader(System.in))).readLine();
        } catch (Exception e) {
            String err = e.toString();
            System.out.println(err);
            inputLine = "";
        }
        return inputLine;
    }

    static String inputString(String prompt) {
        return input(prompt);
    }

    static String input() {
        return input("");
    }

    static int inputInt() {
        return inputInt("");
    }

    static double inputDouble() {
        return inputDouble("");
    }

    static char inputChar(String prompt) {
        char result = (char) 0;
        try {
            result = input(prompt).charAt(0);
        } catch (Exception e) {
            result = (char) 0;
        }
        return result;
    }

    static byte inputByte(String prompt) {
        byte result = 0;
        try {
            result = Byte.valueOf(input(prompt).trim()).byteValue();
        } catch (Exception e) {
            result = 0;
        }
        return result;
    }

    static int inputInt(String prompt) {
        int result = 0;
        try {
            result = Integer.valueOf(
                    input(prompt).trim()).intValue();
        } catch (Exception e) {
            result = 0;
        }
        return result;
    }

    static long inputLong(String prompt) {
        long result = 0;
        try {
            result = Long.valueOf(input(prompt).trim()).longValue();
        } catch (Exception e) {
            result = 0;
        }
        return result;
    }

    static double inputDouble(String prompt) {
        double result = 0;
        try {
            result = Double.valueOf(
                    input(prompt).trim()).doubleValue();
        } catch (Exception e) {
            result = 0;
        }
        return result;
    }

    static boolean inputBoolean(String prompt) {
        boolean result = false;
        try {
            result = Boolean.valueOf(
                    input(prompt).trim()).booleanValue();
        } catch (Exception e) {
            result = false;
        }
        return result;
    }
//=========== end IBIO ===========================================//

}

我的环境:Intellij Community 2016.1.1 + jdk 8 + Windows 7 x64

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM