在java的岩石,纸,剪刀简单的游戏

时间:2014-01-21 03:17:11

标签: java

嗨,我正在制作一个简单的摇滚,纸张,剪刀游戏,并且在让我的比较方法被执行方面遇到了一些麻烦。游戏提示用户输入,然后使用computersTurn方法允许计算机随机选择石头纸或剪刀。当我尝试将这两个值传递给我的比较方法时,它似乎不起作用。任何建议都很棒!

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

2 个答案:

答案 0 :(得分:0)

你无法从来电者那里更新一个字符串(或一个双精灵),所以我认为你应该像这样重新考虑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;
}

然后你可以在main

中做这样的事情
String computerDec = computersTurn();

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

答案 1 :(得分:0)

您的computersTurn方法不需要任何参数。您只需传递" "0.0

尝试将其更改为此方法:

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

然后在main方法中,确保在调用computersTurn()方法时,分配给字符串。叫它对你没有任何意义;你需要保留返回值:

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