在我的代码下面是/否语句不能正常运行,

时间:2015-11-08 00:21:23

标签: java

如果我输入" no"我的问题是是/否。它会继续而不是退出程序。 如果有人可以给我提供问题的提示,请不要这样做?

import java.util.Random; import java.util.Scanner;

公共类NumberGame {     private static final int DO_NOT_PLAY_AGAIN = 0;

private final Scanner mScanner;
private final Random mRandom;
private String mUserName;
private int mCorrectAnswer;
private int mPlayAgainInput;
private String mAnswer;

public NumberGame() {
    mScanner = new Scanner(System.in);
    mRandom = new Random();
}


public void run() {
    displayWelcomeMessage();
    getUserName();
    greetUser();
    getAnswer();


    do {
        intNumberGuessGame();
    } while (mPlayAgainInput != DO_NOT_PLAY_AGAIN);

    sayGoodbye();
}
  private void getAnswer(){
       System.out.println("Would you lioke to play a game enter yes to play or no to exit a game");
        mAnswer = mScanner.nextLine();
        if (mAnswer.equals("no")) 
        System.out.println("Maybe next time");
        sayGoodbye();
  }



private void displayWelcomeMessage() {
    System.out.println("Welcome to the game!");
    System.out.println("To play this game you have to"
                       + " guess a number and enter upon prompt or you can"
                       + " enter 0 to quit the game.");
}

private void getUserName() {
    System.out.println("Enter your user name: ");
    mUserName = mScanner.nextLine();
}

private void greetUser() {
    System.out.println("Let's play a game, " + mUserName + ".");
}

private void sayGoodbye() {
    System.out.println("Thanks for playing, " + mUserName + "!");
}

private void intNumberGuessGame() {
    // Get a random number between 1 - 100
    Random generator = new Random();
    mCorrectAnswer = mRandom.nextInt(100) + 1;
    int theirGuess = 0;
    int howManyTries = 0;
    while (theirGuess != mCorrectAnswer) {
        System.out.println("Guess my number: ");
        theirGuess = mScanner.nextInt();
        mCorrectAnswer = mRandom.nextInt(101) + 1;
        howManyTries++;

        System.out.println("Correct answer = " + mCorrectAnswer);

        if (theirGuess == mCorrectAnswer) {
            System.out.println("You guessed it! It only took you "
                    + howManyTries + " tries to get it right!");
            promptToPlayAgain();
            // They won the game, exit current loop
            break;
        } else if (theirGuess > mCorrectAnswer) {
            System.out.println("Your answer is too high.");
        } else if (theirGuess < mCorrectAnswer) {
            System.out.println("Your answer is too low.");
        }
    }
}

private void promptToPlayAgain() {
    System.out.println("Do you want to play again? (0 to quit): ");
    mPlayAgainInput = mScanner.nextInt();
}

}

public class MainApp {
public static void main(String[] args) {
    new NumberGame().run();
}
}

3 个答案:

答案 0 :(得分:0)

首先,当你检查mAnswer是否等于“no”时,你只打印一个字符串而你总是执行sayGoodbye,因为你缺少大括号。它应该是:

do {
    intNumberGuessGame();
} while (mPlayAgainInput != DO_NOT_PLAY_AGAIN || !mAnswer.equals("no"));

其次,如果你想要是/否问题来决定你是否继续比赛,你需要检查

          ArrayList.get([someindex]).someBookorJournalMethod()

或者在getAnswer()函数中将mPlayAgainInput值设置为0,正好在我前面指出的大括号中。如果选择此选项,则应将do-while块更改为常规while块。

答案 1 :(得分:0)

因为当用户输入“no”时,您只需打印一条消息,而不会实际告诉程序退出。

您可以通过多种不同方式对其进行调整,例如将 private int getAnswer() { System.out.println("Would you like to play a game? Enter yes to play or no to exit a game"); mAnswer = mScanner.nextLine(); if (mAnswer.equals("no")) { System.out.println("Maybe next time"); return DO_NOT_PLAY_AGAIN; } else { return 1; } } 方法更改为:

run

您的 public void run() { displayWelcomeMessage(); getUserName(); greetUser(); mPlayAgainInput = getAnswer(); while (mPlayAgainInput != DO_NOT_PLAY_AGAIN) { intNumberGuessGame(); } sayGoodbye(); } 方法:

08048862 <phase3>:
 8048862:   55                      push   %ebp
 8048863:   89 e5                   mov    %esp,%ebp
 8048865:   83 ec 10                sub    $0x10,%esp
 8048868:   c7 45 fc 00 00 00 00    movl   $0x0,-0x4(%ebp)
 804886f:   83 45 fc 01             addl   $0x1,-0x4(%ebp)
 8048873:   8b 45 fc                mov    -0x4(%ebp),%eax
 8048876:   3b 45 08                cmp    0x8(%ebp),%eax
 8048879:   7e f4                   jle    804886f <phase3+0xd>
 804887b:   81 7d fc 32 06 00 00    cmpl   $0x632,-0x4(%ebp)
 8048882:   0f 94 c0                sete   %al
 8048885:   0f b6 c0                movzbl %al,%eax
 8048888:   c9                      leave  
 8048889:   c3                      ret    

答案 2 :(得分:0)

你的逻辑现在看起来有点乱。我建议你稍微重新设计你的代码。您的问题在于getAnswer()函数的设计方式 - 它会提示答案,但不会使用它。更改此函数,以便它可以返回一个布尔值:

private boolean getAnswer()
{
   System.out.println("Would you lioke to play a game enter yes to play or no to exit a game");
    mAnswer = mScanner.nextLine();

    if (mAnswer.equals("no"))
    {
        System.out.println("Maybe next time");
        sayGoodbye();
        return false;
    }

    return true;
}

run()中使用此结果来检查是否应该开始游戏:

public void run()
{
    displayWelcomeMessage();
    getUserName();
    greetUser();

    if(getAnswer()) //User wants to play!
    {
        do
        {
            intNumberGuessGame();
        } while (mPlayAgainInput != DO_NOT_PLAY_AGAIN);

        sayGoodbye();
    }
}