摇滚,纸,剪刀更新[错误]

时间:2017-05-26 04:33:41

标签: java

所以上周我必须通过Java创建一个Rock,Paper,Scissors游戏。我有几个问题可以在这里澄清一下。不幸的是,我还在另一个果酱。

所以,我试图添加一个功能,以便能够玩电脑而不仅仅是PvP。我创建了一个数组并指示程序随机选择这些选择。然后我使用 if 语句添加了赢/输背后的逻辑。

一切看起来干净而没有错误;直到我继续将我的void方法集成到我的main方法来运行程序。我收到一条错误说明

C:\Users\AVLG2\Documents\NetBeansProjects\R.P.S.game\src\RPSGame.java:104: error: '.class' expected
        compRandom (char P1, char cmpChoice);
C:\Users\AVLG2\Documents\NetBeansProjects\R.P.S.game\src\RPSGame.java:104: error: ';' expected
        compRandom (char P1, char cmpChoice);
C:\Users\AVLG2\Documents\NetBeansProjects\R.P.S.game\src\RPSGame.java:104: error: ';' expected
        compRandom (char P1, char cmpChoice);
3 errors

我仔细检查以确保我拥有所有的&#34 ;;"我回到了我的输赢逻辑,并将if (P1 == 'P' && cmpChoice == 'R')更改为&& cmpChoice == 1)&& cmpChoice == [0])

我觉得我完全误解了这个错误。有什么建议?这是我的代码:

import java.util.Scanner;
public class RPSGame {


    //game mode menu
    public static void gameModeSelect (int modeSelect){     
        System.out.println("Welcome to Rock, Paper, Scissors 1.0 !\n\nPlease select your game mode:\n ");
        System.out.println("1. Player vs. Computer\n2. Player vs. Player \n ");
    }
    //cvp random choice
    public static void compRandom (char P1, char cmpChoice){
        char[] cmp;
        cmp = new char [3];
        cmp[0] = 'R';
        cmp[1] = 'P';
        cmp[2] = 'S';

        cmpChoice = (char) (Math.random()*3);
        System.out.println(cmp[cmpChoice]);
        //game logic CvP  
        if (P1 == 'P' && cmpChoice == 'R'){
            System.out.println("Paper covers rock! Player one wins!");
        } else if (P1 == 'R' && cmpChoice == 'P'){
            System.out.println("Paper covers rock! Computer wins!");
        }    
        if (P1 == 'R' && cmpChoice == 'S'){
            System.out.println("Rock breaks scissors! Player one wins!");
        } else if (P1 == 'S' && cmpChoice == 'R'){
            System.out.println("Rock breaks scissors! Computer wins!");
        }
        if (P1 == 'S' && cmpChoice == 'P'){
            System.out.println("Scissor cuts paper! Player one wins!");
        } else if (P1 == 'P' && cmpChoice == 'S'){
            System.out.println("Scissor cuts paper! Computer wins!");

        } //game logic for same inputs CvP
        if (P1 == 'P' && cmpChoice == 'P'){
            System.out.println("Draw!");
        } if (P1 == 'R' && cmpChoice == 'R'){
            System.out.println("Draw!");
        } if (P1 == 'S' && cmpChoice == 'S'){
        System.out.println("Draw!");
        }
    }

    //game logic PVP
    public static void PvPwinLoss (char P1, char P2){
    //game logic: paper covers rock, rock breaks paper, scissor cuts paper
        if (P1 == 'P' && P2 == 'R'){
             System.out.println("Paper covers rock! Player one wins!");
        } else if (P1 == 'R' && P2 == 'P'){
            System.out.println("Paper covers rock! Player two wins!");
        }    
        if (P1 == 'R' && P2 == 'S'){
            System.out.println("Rock breaks scissors! Player one wins!");
        } else if (P1 == 'S' && P2 == 'R'){
            System.out.println("Rock breaks scissors! Player two wins!");
        }
        if (P1 == 'S' && P2 == 'P'){
            System.out.println("Scissor cuts paper! Player one wins!");
        } else if (P1 == 'P' && P2 == 'S'){
            System.out.println("Scissor cuts paper! Player two wins!");

        } //game logic for same inputs PvP
        if (P1 == 'P' && P2 == 'P'){
            System.out.println("Draw!");
        } if (P1 == 'R' && P2 == 'R'){
            System.out.println("Draw!");
        } if (P1 == 'S' && P2 == 'S'){
            System.out.println("Draw!");
        }

   }
    //game config
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in); 

        //player one and two char variables
        char P1;
        char P2;


        //mode selection variable
        int modeSelect = 0;

        //game mode menu selection output
        gameModeSelect (modeSelect);
        modeSelect = keyboard.nextInt();

        //error for invalid game mode input
        while ((modeSelect >2) || (modeSelect <1)){
            System.out.println("Error: Invalid game mode");
            gameModeSelect (modeSelect);
            modeSelect = keyboard.nextInt();
        }
        //if game mode input valid:
        if (modeSelect == 1){// under construction
            //game rules
            System.out.println("Rules of the game:  R = Rock, P = Paper, S = Scissors\nGood luck!\n ");
            //request player input
            System.out.println("Player one: Enter your move");
            P1 = keyboard.next().charAt(0);
            System.out.println("Computer has chosen a random move . . .");
            compRandom (char P1, char cmpChoice);


        } else if (modeSelect == 2){ // player vs. player break down
            //game rules
            System.out.println("Rules of the game:  R = Rock, P = Paper, S = Scissors\nGood luck!\n ");
            //request player input
            System.out.println("Player one: Enter your move");
            P1 = keyboard.next().charAt(0);
            System.out.println("Player two: Enter your move");
            P2 = keyboard.next().charAt(0);
            PvPwinLoss(P1, P2);//game logic: output - win, lose, or draw
       }       
     }       
}

1 个答案:

答案 0 :(得分:0)

注意:

  1. 首先仔细阅读错误消息。并修复你的语法错误 可以轻松理解:

    ;

    在这里你错过了一个分号(C:\Users\AVLG2\Documents\NetBeansProjects\R.P.S.game\src\RPSGame.java:104: error: '.class' expected compRandom (char P1, char cmpChoice); )。首先尝试解决这些问题。

  2. 格式化整个代码行。使用适当的缩进,空行和命名约定。即使对我们来说,这些也会让您轻松浏览代码。

  3. 我认为您在此无法理解的错误是:

    compRandom (char P1, char cmpChoice)

    if (modeSelect == 1){// under construction //game rules System.out.println("Rules of the game: R = Rock, P = Paper, S = Scissors\nGood luck!\n "); //request player input System.out.println("Player one: Enter your move"); P1 = keyboard.next().charAt(0); System.out.println("Computer has chosen a random move . . ."); compRandom (char P1, char cmpChoice); } 是您实施的方法,它有两个参数。调用方法时,必须给出两个参数来匹配这两个参数。

    但你所做的是,当你调用方法而不是给出值(参数)时,你试图声明两个变量(在调用方法中)无法完成,编译器会感到困惑。

    看最后一行:

    compRandom (char P1, char cmpChoice);

    而不是这个compRandom (P1, 'c'); 尝试传递两个值或初始化变量。

    像:

    'c'

    import UIKit enum TypeOfDay { case weekday case weekend } class TabBarViewController: UITabBarController { var typeOfDay: TypeOfDay = .weekday override func viewDidLoad() { super.viewDidLoad() // Initial order is: WeekendVC, WeekdayVC, OtherVC if let currentViewControllers = self.viewControllers { let weekendVC = currentViewControllers[0] let weekdayVC = currentViewControllers[1] let otherVC = currentViewControllers[2] switch typeOfDay { case .weekend: self.viewControllers = [weekendVC, otherVC] case .weekday: self.viewControllers = [weekdayVC, otherVC] } } } } 向您展示。你可以放任何你想要的东西。

    在我看来,你复制了这个方法而忘了改变它。