简体   繁体   中英

Using for loop for Rock Paper Scissors Game Java

I have been interested in Java for around 2 months now as I have done some courses online, I'm trying to create a game for Rock Paper Scissors. I want the game to be able to run 5 times by using the for loop, though I'm having trouble looping it; it keeps declaring the winner instead of repeating the game?

import java.util.Scanner;
import java.io.*;

public class Part2
{

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

        String userInput1 = " ";
        char player1Input;

        String userInput2 = " ";
        char player2Input;

        for(int i = 0; i < 5; i++) //Loop for game
        {

        System.out.println("Player 1: Please Enter e.g R for Rock:");
        System.out.println("R.Rock");
        System.out.println("P.Paper");
        System.out.println("S.Scissors");

        userInput1 = scan.next();
        player1Input = userInput1.charAt(0);


        System.out.println("Player 2: Please Enter e.g S for Scissors");
        System.out.println("R.Rock");
        System.out.println("P.Paper");
        System.out.println("S.Scissors");



        userInput2 = scan.next();
        player2Input = userInput2.charAt(0);

        }





        switch(player1Input)
        {
            case 'R':  System.out.println ("Player 1:Rock");

                switch(player2Input)
                {
                    case'R' : System.out.println("Player 2:Rock");
                    System.out.println("The Game Result is Draw");
                    break;
                    case'P' :System.out.println("Player 2:Paper");
                    System.out.println("The Game Result is: Player 2 Wins");
                    break;
                    case'S' :System.out.println("Player 2:Scissors");
                    System.out.println("The Game Result is: Player 1 Wins");
                    break;
                    default: System.out.println("Invalid Option");
                    break;
                }
            break;

            case 'P' : System.out.println ("Player 1:Paper");
                switch(player2Input)
                {
                    case'R' : System.out.println("Player 2:Rock");
                    System.out.println("The game result is: Player 1 Wins");
                    break;
                    case'P' :System.out.println("Player 2:Paper");
                    System.out.println("The game result is: Draw");
                    break;
                    case'S' :System.out.println("Player 2:Scissors");
                    System.out.println("The Game Result is: Player 2 Wins2");
                    break;
                    default: System.out.println("Invalid Option");
                    break;
                }
            break;

            case 'S' : System.out.println ("Player 1:Scissors");
                switch(player2Input)
                {
                    case'R' : System.out.println("Player 2:Rock");
                    System.out.println("The Game Result is: Player 2 Wins");
                    break;
                    case'P' :System.out.println("Player 2:Paper");
                    System.out.println("The game result is: Player 1 Wins");
                    break;
                    case'S' :System.out.println("Player 2:Scissors");
                    System.out.println ("The Game Result is: Draw");
                    break;
                    default: System.out.println("Invalid Option");
                    break;
                }
            break;

        }
    }
}

Your switch statement is out of the code executed by the loop, move it to the point right after the line player2Input = userInput2.charAt(0); , it must stay inside the loop brackets.

The switch statement is out of scope, which means that the program doesn't know where to look for it and for all intents and purposes, it's invisible to the program and the loop. If you move the bracket after the for loop to after the switch statement, it should run just fine.

The switch statement you wrote only evaluates the last input from the players, so it needs to be moved in the for loop.

Also, it is always good to initialize your variables when you declare them.

import java.util.Scanner;
public class Part2
{
    public static void main (String[]args)
    {
        Scanner scan = new Scanner(System.in);

        String userInput1 = "";
        char player1Input = ' ';

        String userInput2 = "";
        char player2Input = ' ';

        for(int i = 0; i < 5; i++) //Loop for game
        {
            System.out.println("Player 1: Please Enter e.g R for Rock:");
            System.out.println("R.Rock");
            System.out.println("P.Paper");
            System.out.println("S.Scissors");

            userInput1 = scan.next();
            player1Input = userInput1.charAt(0);


            System.out.println("Player 2: Please Enter e.g S for Scissors");
            System.out.println("R.Rock");
            System.out.println("P.Paper");
            System.out.println("S.Scissors");



            userInput2 = scan.next();
            player2Input = userInput2.charAt(0);

            switch(player1Input)
            {
                case 'R':  System.out.println ("Player 1:Rock");
                    switch(player2Input)
                    {
                        case'R' :
                            System.out.println("Player 2:Rock");
                            System.out.println("The Game Result is Draw");
                            break;
                        case'P' :
                            System.out.println("Player 2:Paper");
                            System.out.println("The Game Result is: Player 2 Wins");
                            break;
                        case'S' :
                            System.out.println("Player 2:Scissors");
                            System.out.println("The Game Result is: Player 1 Wins");
                            break;
                        default:
                            System.out.println("Invalid Option");
                            break;
                    }
                    break;

            case 'P' : System.out.println ("Player 1:Paper");
                switch(player2Input)
                {
                    case'R' :
                        System.out.println("Player 2:Rock");
                        System.out.println("The game result is: Player 1 Wins");
                        break;
                    case'P' :
                        System.out.println("Player 2:Paper");
                        System.out.println("The game result is: Draw");
                        break;
                    case'S' :
                        System.out.println("Player 2:Scissors");
                        System.out.println("The Game Result is: Player 2 Wins2");
                        break;
                    default:
                        System.out.println("Invalid Option");
                        break;
                }
                break;

            case 'S' : 
                System.out.println ("Player 1:Scissors");
                switch(player2Input)
                {
                    case'R' :
                        System.out.println("Player 2:Rock");
                        System.out.println("The Game Result is: Player 2 Wins");
                        break;
                    case'P' :
                        System.out.println("Player 2:Paper");
                        System.out.println("The game result is: Player 1 Wins");
                        break;
                    case'S' :
                        System.out.println("Player 2:Scissors");
                        System.out.println ("The Game Result is: Draw");
                        break;
                    default:
                        System.out.println("Invalid Option");
                        break;
                }
                break;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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