简体   繁体   English

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

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

Ok so I'm making a rock paper scissors program in java and I have all the code and it should give me the output I want but every time i run it it only prints out the input statement and nothing else.好的,所以我正在用 java 制作一个石头剪刀布程序,我有所有的代码,它应该给我我想要的输出,但每次我运行它时,它只打印输入语句,没有别的。 I made it print a line after the do loop that the input statement is located and it prints that but it doesn't print anything that's in the if statements and I don't quite understand why.我让它在输入语句所在的 do 循环之后打印一行并打印出来,但它不打印 if 语句中的任何内容,我不太明白为什么。 This is what I have.这就是我所拥有的。 Thank you for any help provided.感谢您提供的任何帮助。

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.

相关问题 为什么我的程序在用户输入后打印了 3 次? - Why is my program printing 3 times after user input? 为什么我的程序不打印我想要的内容? - Why isnt my program printing out what i want it to? 无法弄清楚为什么我的程序不打印数组内容 - Can't figure out why my program is not printing contents of array 为什么我的程序在打印出我的 letterList 数组时会跳过数字? 它只是打印出 AZ 和 az - Why does my program skip over digits when printing out my letterList array? It is only printing out A-Z and a-z 我无法弄清楚为什么我的程序在运行时没有打印 - I can't figure out why my program isn't printing when I run it 当我通过方法将它作为输入时,为什么我的 java 程序在 output 中为字符串打印“null”? - why is my java program printing "null" in output for a string when I took it as an input through a method? 为什么我的程序打印“ null”而不是字符串? - Why is my program printing “null” instead of the string? 为什么我的MadLib txt文件程序在右括号“]”之后的每个生命中都无法打印任何内容 - Why is my MadLib txt file program not printing anything on each life after the right bracket “]” 为什么我的for循环对象无法打印出来? - Why isnt my for loop of objects printing out? 为什么不打印我的学生字符串? - Why arent my Student strings printing out?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM