简体   繁体   English

使用Rock剪刀纸游戏Java的for循环

[英]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. 我已经对Java感兴趣了大约2个月了,因为我已经在线上完成了一些课程,我正在尝试为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; 我希望游戏可以通过使用for循环运行5次,尽管我无法循环执行它; 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); 您的switch语句不在循环执行的代码中,将其移到player2Input = userInput2.charAt(0);行之后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. switch语句超出范围,这意味着程序不知道在哪里寻找它,并且就所有意图和目的而言,它对于程序和循环都是不可见的。 If you move the bracket after the for loop to after the switch statement, it should run just fine. 如果将for循环后的括号移动到switch语句之后,则该括号应运行良好。

The switch statement you wrote only evaluates the last input from the players, so it needs to be moved in the for loop. 您编写的switch语句仅评估来自播放器的最后输入,因此需要在for循环中移动它。

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;
            }
        }
    }
}

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

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