为什么我的程序在输入后没有打印出来?

时间:2017-10-05 16:10:46

标签: java

好的,所以我在java中制作一个石头剪刀程序,我有所有的代码,它应该给我我想要的输出,但每次我运行它只打印输入语句,没有别的。我在输入语句所在的do循环之后打印了一行,并打印出来但是它没有打印if语句中的任何内容,我不太明白为什么。这就是我所拥有的。感谢您提供的任何帮助。

public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);
    Random rand = new Random();

    String replay = ("");
    String user = ("");
    int ai = 0;
    String aiPlay = ("");

    do
    {
        ai = rand.nextInt(3) + 1;

        switch (ai)    //was if statements but java suggested to change to switch statements
        {
            case 1:
                aiPlay = ("Rock");
                break;
            case 2:
                aiPlay = ("Paper");
                break;
            default:
                aiPlay = ("Scissors");
                break;
        }

        do   //make sure input is correct
        {
            System.out.print("rock, paper, or scissors: ");
            user = input.next();
        }while(!(user.equals("rock") || user.equals("paper") || user.equals("scissors")));

        if(user.equals(ai))
        {
        System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nIt was a tie!");
        System.out.print("Continue? (y or n): ");
        replay = input.next();
        }
        switch (user) {    //was if statements java suggested it change to switch statement 
            case "rock":
                if (aiPlay.equals("scissors"))
                {
                    System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou win!");
                    System.out.print("Continue? (y or n): ");
                    replay = input.next();
                }

                else if (aiPlay.equals("paper"))
                {
                    System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou lose!");
                    System.out.print("Continue? (y or n): ");
                    replay = input.next();
                }   break;
            case "paper":
                if (aiPlay.equals("scissors"))
                {
                    System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou win!");
                    System.out.print("Continue? (y or n): ");
                    replay = input.next();
                }

                else if (aiPlay.equals("rock"))
                {
                    System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou lose!");
                    System.out.print("Continue? (y or n): ");
                    replay = input.next();
                }   break;
            case "scissors":
                if (aiPlay.equals("paper"))
                {
                    System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou win!");
                    System.out.print("Continue? (y or n): ");
                    replay = input.next();
                }

                else if (aiPlay.equals("rock"))
                {
                    System.out.println("You played " + user + ".\nThe computer player " + aiPlay + ".\nYou lose!");
                    System.out.print("Continue? (y or n): ");
                    replay = input.next();
                }   break;
            default:
                break;
        }

    }while(replay.equals("y"));
}

1 个答案:

答案 0 :(得分:0)

switch (ai)    //was if statements but java suggested to change to switch 
statements
    {
        case 1:
            aiPlay = ("rock"); /* All should be lowercase*/ 
            break;
        case 2:
            aiPlay = ("paper"); /* All should be lowercase*/ 
            break;
        default:
            aiPlay = ("scissors"); /* All should be lowercase*/ 
            break;
    }