繁体   English   中英

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

[英]Why is my program not printing out after the input?

好的,所以我正在用 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"));
}
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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM