石头,纸,剪刀游戏experimetal

时间:2018-10-08 09:26:49

标签: java

大家好,所以我想在玩家和机器之间创建一个石头,纸,剪刀的游戏,然后根据结果对玩家和机器进行评分。问题是我的分数总是等于0,我听不懂! 请帮助大家,我也是新手...代码在这里

import javax.swing.*;

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

public class Game2

{

    //Declaring my random object

    private static Random random = new Random();


    public static void main(String[] args)
    {

        //Declaring my variables
        int playerScore = 0;
        int machineScore = 0;

        int totPlayerScore = 0;
        int totMachineScore = 0;

        //Instantiating my list object
        List<String> dice = new ArrayList<>();

        //We gon rename scissors to cuts for a shorter input
        //Adding values to the list
        dice.add("rock");
        dice.add("paper");
        dice.add("cuts");


        //Paper beats rock, rock beats cuts, cuts beats paper
        //For loop to play the game 3 times
        for(int i = 0; i <3; i++)
        {

            //User input
            String strInput = JOptionPane.showInputDialog("Please enter paper, rock or cuts");


            //Randomizing the list
            int index = random.nextInt(dice.size());

            //If statement to see if the usr or machine won
            if(strInput.toLowerCase() == "paper" && dice.get(index) == "rock")//win
            {

                playerScore += 1;

                totPlayerScore += playerScore;
            }
            else if(strInput.toLowerCase() == "rock" && dice.get(index) == "cuts")//win
            {
                playerScore += 1;

                totPlayerScore += playerScore;
            }
            else if(strInput.toLowerCase() == "cuts" && dice.get(index) == "paper")//Win
            {
                playerScore += 1;

                totPlayerScore += playerScore;
            }
            else if(strInput.toLowerCase() == "paper" && dice.get(index) == "paper")//Draw
            {
                playerScore = playerScore;

                totPlayerScore += playerScore;
            }
            else if(strInput.toLowerCase() == "rock" && dice.get(index) == "rock")//Draw
            {
                playerScore = playerScore;

                totPlayerScore += playerScore;
            }
            else if(strInput.toLowerCase() == "cuts" && dice.get(index) == "cuts")//Draw
            {
                playerScore = playerScore;

                totPlayerScore += playerScore;

            }//End of player if statement

            //Start of machine if statement
            if(strInput.toLowerCase() == "rock" && dice.get(index) == "paper")//win
            {

                machineScore += 1;

                totMachineScore += machineScore;
            }
            else if(strInput.toLowerCase() == "cuts" && dice.get(index) == "rock")//win
            {
                machineScore += 1;

                totMachineScore += machineScore;
            }
            else if(strInput.toLowerCase() == "paper" && dice.get(index) == "cuts")//Win
            {
                machineScore += 1;

                totMachineScore += machineScore;
            }

             //End of if statement


            //Displaying user Results
            System.out.println("User draw: " + strInput.toLowerCase() + " Machine draw: " + dice.get(index).toLowerCase());
            System.out.println("User Score " + playerScore + " Machine Score " + machineScore +  "\n");


        }//End of for loop


        //Displaying my final results
        System.out.println("User Final score: " + totPlayerScore + " Machine Final score: " + totMachineScore + "\n");


        //If statement to see who won, hte player or machine
        if(totPlayerScore > totMachineScore)
        {
            System.out.println("The Player won!!!");
        }
        else if(totPlayerScore == totMachineScore)
        {
            System.out.println("Its a draw!!!");
        }
        else
        {
            System.out.println("The Machine won!!! \n");
        }

        //Console message to end the program and game
        System.out.println("Game over!!!!!");

    }//End of main

}//End of class

0 个答案:

没有答案