Java:如何创建重播功能

时间:2018-10-15 00:49:59

标签: java new-operator

我如何做到这一点,以便该程序提示用户再次播放? 我猜我需要做一个do-while循环,如果重复'Y',如果'N'exit.program。但是,我不确定如何。我在这里看到了其他类似的问题,但都是专门针对其代码完成的。我还很新,所以我不确定如何将其翻译成我自己的内容。

公共类Project3 {

public static void main(String[] args) {
    //Random Number Generator
    Random random = new Random();

    Scanner scanner = new Scanner(System.in);

    int user, jerkAi;

    //prompts user for input
    System.out.println("Hello, you're probably here because you have no friends. \nThus you have no one to play " +
            "Rock, Paper, Scissors with.\nSo I'm here to play with you...maybe not be your friend, but I'll play \n\n" +
            "Please Enter a Move: (1 = Rock; 2 = Paper; 3 = Scissors):");

    //stores input
    user = scanner.nextInt();

    //checks for user error
    if (user < 1 || user > 3) {
        System.out.println("Are you stupid? I literally told you what to enter. \n" +
                "Sorry...this isn't gonna work out, good bye");

        //Exits Program
        System.exit(0);
    }

    //jerkAi
    jerkAi = random.nextInt(3);

    //Checks if results are the same
    if (user == jerkAi)
    {
        if (user == 1) {
            System.out.println("Why would you pick Rock? That is what I picked...uhg...it's a tie.");
        }
        if (user == 2) {
            System.out.println("Why would you pick Paper? That is what I picked...uhg...it's a tie.");
        }
        if (user == 3) {
            System.out.println("Why would you pick Scissors? That is what I picked...uhg...it's a tie.");
        }

        //Exits Program
        System.exit(0);
    }

    // 1 == Rock; User Picks Rock
    if(user == 1)
    {
        if(jerkAi == 2)
        {
            System.out.println("Paper wraps Rock, better luck next time.");
            System.out.println("Jerk AI wins the Match!");
        }
        else
        {
            System.out.println("Rock smashes the Scissors...Wow. Ok, you win, whatever.");
            System.out.println("You Win!");
        }
    }

    // 2 == Paper; User picks Paper
    if(user == 2)
    {
        if(jerkAi == 1)
        {
            System.out.println("Paper wraps Rock...Wow. Ok, you win, whatever.");
            System.out.println("You Win!");
        }
        else
        {
            System.out.println("Scissors cuts Paper, better luck next time.");
            System.out.println("Jerk AI wins the Match!");
        }
    }
    // 3 == Scissors; User picks Scissors
    if(user == 3)
    {
        if(jerkAi == 1)
        {
            System.out.println("Rock smashes the Scissors, better luck next time.");
            System.out.println("Jerk AI wins the Match!");
        }
        else
        {
            System.out.println("Scissors cuts Paper...Wow. Ok, you win, whatever.");
            System.out.println("You Win!");
        }
    }
    scanner.close();
}

}

0 个答案:

没有答案