简体   繁体   中英

Rock, paper, scissors simple game in java

Hi I am making a simple rock, paper, scissors game and was having some trouble getting my compare method to get executed. The game prompts the user for input, and then using the computersTurn method to allow the computer to randomly select rock paper or scissors. When I try and pass both these values into my compare method, it doesn't seem to work. Any suggestions would be awesome!

import java.util.Scanner;



public class sillyGame {

public static void compare (String choice1, String choice2)

{

    if (choice1.equals(choice2))

    {

    System.out.println("The result is a tie!");   

    }



    if (choice1.contains("rock"))

    {
        if (choice2.contains("scissors"))
        {
            System.out.println("rock wins");
        }

        if (choice2.contains("paper"))
        {
            System.out.println("paper wins");
        }
    }   



        if (choice1.contains("scissors"))
    {
        if (choice2.contains("rock"))
        {
            System.out.println("rock wins");
        }

        if (choice2.contains("paper"))
        {
            System.out.println("scissors wins");
        }

    }



    if (choice1.contains("paper"))

    {
        if (choice2.contains("rock"))
        {
            System.out.println("paper wins");
        }

        if (choice2.contains("scissors"))
        {
            System.out.println("scissors wins");
        }

    }




}

public static String computersTurn(String compFinalChoice, double randomNum){

randomNum = Math.random();


    if (randomNum < 0.34) 
    {
        compFinalChoice = "rock";

    } 

    else if(randomNum <= 0.67) 
    {
        compFinalChoice = "paper";

    } 

    else 
    {
        compFinalChoice = "scissors";

    }

       System.out.println("The computer chooses " + compFinalChoice);
      return compFinalChoice;
}



public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("Do you choose rock, paper or scissors?");
    String userChoice = scan.nextLine();
    String computerDec = " ";
    double rand = 0.0;
    computersTurn(computerDec, rand);
    compare(userChoice, computerDec); 
}

You cannot update a String (or a double) from a caller so I think you should re-factor computersTurn like so

public static String computersTurn() {
  String choice = "scissors";
  double randomNum = Math.random();
  if (randomNum < 0.34) {
    choice = "rock";
  } else if (randomNum <= 0.67) {
    choice = "paper";
  }
  System.out.println("The computer chooses "
      + choice);
  return choice;
}

Then you can do something like this in main

String computerDec = computersTurn();

or

String userChoice = scan.nextLine();
compare(userChoice, computersTurn());

Your computersTurn method doesn't need any parameters. You're just passing it " " and 0.0 .

Try changing it to this method:

public static String computersTurn() {

    // declare them here
    double randomNum = Math.random();
    String compFinalChoice = "";

    if (randomNum < 0.34) {
        compFinalChoice = "rock";
    }
    else if(randomNum <= 0.67) {
        compFinalChoice = "paper";
    } 
    else {
        compFinalChoice = "scissors";
    }

    System.out.println("The computer chooses " + compFinalChoice);
    return compFinalChoice;
}

And then in your main method, make sure when you call the computersTurn() method, you assign it to a String. Calling it does nothing for you; you need to keep the return value:

public static void main(String args[]) 
{
    Scanner scan = new Scanner(System.in);
    System.out.println("Do you choose rock, paper or scissors?");
    String userChoice = scan.nextLine();
    String computerDec = computersTurn(); // assign it here
    compare(userChoice, computerDec);
}

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