简体   繁体   English

Java中的石头,纸,剪刀简单游戏

[英]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. 游戏提示用户进行输入,然后使用computersTurn方法允许计算机随机选择石头纸或剪刀。 When I try and pass both these values into my compare method, it doesn't seem to work. 当我尝试将这两个值都传递给我的compare方法时,它似乎不起作用。 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 您不能从调用方更新字符串(或双精度字符串),所以我认为您应该重构computersTurn

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 然后您可以在main执行类似的操作

String computerDec = computersTurn();

or 要么

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

Your computersTurn method doesn't need any parameters. 您的computersTurn方法不需要任何参数。 You're just passing it " " and 0.0 . 您只需传递" "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. 然后在您的main方法中,确保在调用computersTurn()方法时分配给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);
}

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

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