Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么输入后我的程序没有打印出来?_Java - Fatal编程技术网

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

Java 为什么输入后我的程序没有打印出来?,java,Java,好的,我正在用java制作一个石头剪刀程序,我有所有的代码,它应该会给我想要的输出,但每次我运行它时,它只打印出输入语句,其他什么都没有。我让它在do循环之后打印一行,输入语句所在的位置,然后打印出来,但它不打印if语句中的任何内容,我不太明白为什么。这就是我所拥有的。谢谢你提供的任何帮助 public static void main(String[] args) { Scanner input = new Scanner(System.in); Random rand =

好的,我正在用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语句中移出(最好移到循环的末尾),因为它总是相同的。还可以查看这些行:
aiPlay=(“Rock”)
aiPlay.equals(“Rock”)
-看到问题了吗?因此,AI执行“Rock”、“Paper”或“剪刀”,而用户执行“Rock”、“Paper”或“剪刀”。这些选择中有任何一个是平等的吗?谢谢你,克里斯。我看了所有的东西,想弄明白为什么它刚刚结束。我只是忘了大写字母和ai的关系。非常感谢。
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;
    }