【问题标题】:In my code below Yes/No statement is not functioning properly,在我下面的代码中是/否语句无法正常运行,
【发布时间】:2016-02-08 22:22:54
【问题描述】:

我遇到的问题是是/否语句,如果我输入“否”,它将继续退出程序。 请如果有人可以给我提示问题在哪里?

导入 java.util.Random; 导入 java.util.Scanner;

公开课 NumberGame { 私有静态最终 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();
}
}

【问题讨论】:

  • 你的哨兵是零,基于mPlayAgainInput = mScanner.nextInt();。你为什么假设如果它扫描为“否”就会退出游戏?
  • 这是我有问题的部分,private void getAnswer(){ System.out.println("你想玩游戏输入是玩还是不退出游戏"); mAnswer = mScanner.nextLine(); if (mAnswer.equals("no")) System.out.println("也许下次");说再见(); }

标签: java


【解决方案1】:

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

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

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

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

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

【讨论】:

    【解决方案2】:

    因为当用户输入“否”时,您只是打印一条消息,而从未真正告诉程序退出。

    您可以通过多种不同方式对其进行调整,例如将getAnswer 方法更改为:

        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();
        }
    

    【讨论】:

    • 非常感谢 Ivaylo Toskov。
    • @DKCroat 一旦有人帮助了你,不要只是走开。通过投票回答并使用复选标记接受最佳答案来表示感谢。
    • 我当然会做@BLaZuRE。我很抱歉这是我第一次使用这个网站。谢谢你的意见:)
    • 感谢大家对我的问题提出意见。
    【解决方案3】:

    你的逻辑现在看起来有点混乱。我会建议你重新设计你的代码一点点。您的问题在于 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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 2020-10-04
      相关资源
      最近更新 更多