【问题标题】:Java play again? (y/n) [duplicate]又玩Java了? (是/否)[重复]
【发布时间】:2016-05-25 15:57:06
【问题描述】:
import java.util.Scanner;
public class PlayAgain {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);           
    boolean playing = true;
    char replayCheck;

    do { //start do-while
        System.out.print("Play again? (y/n): ");           
        boolean validInput = false;
        while (validInput = false){ //start while
            replayCheck = input.next().charAt(0);
            switch (replayCheck) { //start switch
                case 'y':
                case 'Y':
                    validInput = true;
                    playing = true;
                    break;
                case 'n':
                case 'N':
                    validInput = true;
                    playing = false;
                    break;
                default:
                    System.out.println("Invalid input! please enter (y/n)");
                    validInput = false;
                    break;
            } //end switch
        } //end while
    } while (playing = true); //end do-while     
    System.out.println("Thanks for playing!");
} //end main
} //end class

如果用户输入n/N,程序会再次播放,任何其他输入也是如此。逻辑似乎很好,但我在replayCheck = input.next().charAt(0); 的行上得到“从未使用分配的值”,所以我怀疑问题就在那里。

我有点菜鸟。欢迎提出任何建议!

【问题讨论】:

  • 你的while测试错了:你做作而不是测试...替换为:playing == true

标签: java


【解决方案1】:

检查需要:

while (validInput == false) {
    ....
}

否则,您会将false 分配给validInput,这会导致false 并因此退出循环。

在 Java 中,编写此类检查的惯用方式是:

while (!validInput) {
    ...
}

【讨论】:

  • 哈哈!简单的错误总是我最头疼的。工作就像一个魅力,谢谢!
【解决方案2】:

问题出在 while 循环中,应该是 while (validInput == false) {}

【讨论】:

    【解决方案3】:

    将 '=' 更改为 '==' 进行比较,您的代码运行良好:

    import java.util.Scanner;
    public class PlayAgain {
    public static void main(String[] args) {
    
        Scanner input = new Scanner(System.in);
        boolean playing = true;
        char replayCheck;
    
        do { //start do-while
            System.out.print("Play again? (y/n): ");
            boolean validInput = false;
            while (validInput == false){ //start while
                replayCheck = input.next().charAt(0);
                switch (replayCheck) { //start switch
                    case 'y':
                    case 'Y':
                        validInput = true;
                        playing = true;
                        break;
                    case 'n':
                    case 'N':
                        validInput = true;
                        playing = false;
                        break;
                    default:
                        System.out.println("Invalid input! please enter (y/n)");
                        validInput = false;
                        break;
                } //end switch
            } //end while
        } while (playing == true); //end do-while
        System.out.println("Thanks for playing!");
    } //end main
    } //end class
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2011-05-16
      • 2012-05-13
      • 2017-11-08
      • 1970-01-01
      • 2012-08-30
      • 2021-06-11
      相关资源
      最近更新 更多