我用什么java代码在cmd中退出程序或结束程序?

polhcujo  于 2021-07-11  发布在  Java
关注(0)|答案(5)|浏览(344)

如何结束我的节目?
我尝试了3次让用户回答某个答案,但是如果用户回答不正确,我就使用了所有的3次尝试。我怎样才能结束这个节目?
这是我的密码:

while (attempts1-- > 0 && !answer1.equals(ans1))
        {
            System.out.print("What is your guess? ");
            ans1 = sc.next();

            if (ans1.equals(answer1)) {
                System.out.println("You guessed correctly!");
                attempts1 = 3;
            }
            else {
                System.out.println("Incorrect. Attempts remaining: " + attempts1 + " What is your guess again?");
            }
            if (attempts1 == 0) {
                System.out.println("Game over..");
            }
        }

游戏结束后,程序仍然继续下一个问题,即使游戏应该结束。代码是什么?

5cnsuln7

5cnsuln71#

你可以用

System.exit(0);

这个 0 表示程序正常退出,如果有异常终止或您可以使用的东西 1 或者其他特定的非零状态码。

8wtpewkr

8wtpewkr2#

你需要这么做 break . 我已经把这个加进去了 if ( attempts1 == 0 ) . 现在应该可以解决你的问题了。

while( attempts1-- > 0 && !answer1.equals( ans1 ) ) {
            System.out.print( "What is your guess? " );
            ans1 = sc.next();

            if ( ans1.equals( answer1 ) ) {
                System.out.println( "You guessed correctly!" );
                attempts1 = 3;
            }
            else {
                System.out.println( "Incorrect. Attempts remaining: " + attempts1 + " What is your guess again?" );
            }
            if ( attempts1 == 0 ) {
                System.out.println( "Game over.." );
                break;
            }
        }
hs1ihplo

hs1ihplo3#

您试图在循环中用正确答案检查输入的值,因此每次将其添加到循环中进行检查是多余的:
您可以使用找到正确答案后关闭应用程序 break 关键字或3错误尝试:

while (attempts1-- > 0) {
    System.out.print("What is your guess? ");
    ans1 = sc.next();

    if (ans1.equals(answer1)) {
        System.out.println("You guessed correctly!");
        break;
    } else {
        System.out.println("Incorrect. Attempts remaining: " + attempts1 + " What is your guess again?");
    }

    if (attempts1 == 0) {
        System.out.println("Game over..");
        System.exit(0);
    }
}
cxfofazt

cxfofazt4#

根据你发布的代码,我猜还有另一个更大的循环问题。所以你也需要用旗子来阻止更大的循环。比如说:

for (String answer1: answers) {
        boolean gameOver = false;
        while (attempts1-- > 0 && !answer1.equals(ans1))
        {
            System.out.print("What is your guess? ");
            ans1 = sc.next();

            if (ans1.equals(answer1)) {
                System.out.println("You guessed correctly!");
                attempts1 = 3;
            }
            else {
                System.out.println("Incorrect. Attempts remaining: " + attempts1 + " What is your guess again?");
            }
            if (attempts1 == 0) {
                System.out.println("Game over..");
                gameOver = true;
            }
        }
        if (gameOver) {
            break;
        }
    }
tvmytwxo

tvmytwxo5#

有很多关于使用system.exit(…)的建议—从技术上讲,如果有充分的理由退出应用程序,这是正确的方法。然而,在您的例子中,您有一个应该退出的循环,但它不起作用—这表示问题出在您的循环条件中。
不管是中断还是系统,exit(…)调用只是绕过了在其他地方确实存在问题的事实。
也就是说,如果我按原样运行你的代码,它就会按预期运行。那么还有什么代码是我们看不到的呢?

相关问题