岩石剪刀简单的java使用方法

时间:2016-02-27 02:30:55

标签: java

所以今天我的任务是使用方法制作一个石头剪刀游戏。我的代码的第一个问题是我需要它来询问用户是否想要再次播放。 (是或否)另外,我需要实施评分,如果用户获胜则为1分,如果他们输了则为-1,并且每次他们再次游戏时将该分数添加到总分。有关如何实现这一点的任何想法?或者我需要改变我的代码。此外,我是一个菜鸟,对于丑陋的格式感到抱歉,随时可以批评每一个细节,生病了所有的信息。

 public static void main(String[] args){
 boolean tie = true;
    do{
        String computer = computerChoice();
        String user = userChoice();
        tie = (computer.compareTo(user) == 0);
        determineWinner(computer, user);
    }while(tie);

  }

  public static String computerChoice( ){
  Random rand = new Random();
 int cinput = rand.nextInt(3)+ 1;
  String computer = "thing";
  if (cinput == 1)
  computer = "Rock";
   if (cinput == 2)
  computer = "Paper";
 if (cinput == 3)
  computer = "Scissors";
    return computer;
  }
  public static String userChoice(){
  Scanner sc = new Scanner (System.in);
  String user = "default";
  do{
    System.out.println("choose your weapon(Paper,Scissors or Rock)");
    user = sc.nextLine();
  }
  while (isValidChoice (user) == false);
  return user;
  }
  public static boolean isValidChoice(String choice){
  boolean status;
  if (choice.compareTo("Rock")== 0)
    status = true;
  else if (choice.compareTo("Paper")== 0)
    status = true;
  else if (choice.compareTo("Scissors")== 0)
    status = true;
  else{
    status = false;
    System.out.println("Error! Make sure you are capitalizing your                    choices");
}

return status;
  }
  public static boolean determineWinner(String computer, String user){
  System.out.println (" Computer Choice: " + computer);
  System.out.println ("Your Choice : " + user);
  if (computer.compareTo( "Rock" ) == 0 && user.compareTo  ("Scissors") == 0)
  System.out.println (" Computer wins! Better luck next time!");
  if (computer.compareTo("Scissors")== 0 && user.compareTo("Paper") == 0)
  System.out.println (" Computer wins! Better luck next time!");
  if (computer.compareTo("Paper") == 0 && user.compareTo("Rock") == 0)
  System.out.println (" Computer wins! Better luck next time!");
   if (computer.compareTo("Rock") == 0 && user.compareTo("Paper") == 0)
   System.out.println (" You win!!");
  if (computer.compareTo("Scissors") == 0 && user.compareTo("Rock") == 0)
  System.out.println (" You win!!");
  if (computer.compareTo("Paper") == 0 && user.compareTo("Scissors") == 0)
  System.out.println (" You win!!");
  else if (computer.compareTo(user) == 0 ){
    System.out.println(" Tie! the game must be played again.");
    return false;
  }
  return true;
}
}

我教授给我们的一个输出是:

Choose your weapon!
1. Paper
2. Scissors
3. Rock
5
 Choose your weapon!
 1. Paper
 2. Scissors
 3. Rock
  1
 You chose paper!
 I choose rock!
 I have been vanquished!
 We have matched wits 1 times, and your score is 1
 Do you want to play again (y or n)? y
 Choose your weapon!
 1. Paper
 2. Scissors
 3. Rock
 1
 You chose paper!
  I choose paper!
  We are equally matched. You are a worthy adversary.
  We have matched wits 2 times, and your score is 1
  Do you want to play again (y or n)? n

1 个答案:

答案 0 :(得分:2)

这是完成的代码。我添加了两个功能,一个用于调用实际游戏,另一个用于检查玩家是否想再玩一次。此外,最后还有结束语

import java.util.Random;
import java.util.Scanner;

public class RPC
{
    public static Scanner   sc          = new Scanner(System.in);
    public static int       score       = 0;
    public static int       gameCount   = 0;

    public static void main(String[] args)
    {
        play();
        while (playAgain())
        {
            play();
        }
    }

    public static void play()
    {
        String computer = computerChoice();
        String user = userChoice();
        determineWinner(computer, user);
    }

    public static String computerChoice()
    {
        Random rand = new Random();
        int cinput = rand.nextInt(3) + 1;
        String computer = "thing";
        if (cinput == 1)
            computer = "Rock";
        if (cinput == 2)
            computer = "Paper";
        if (cinput == 3)
            computer = "Scissors";
        return computer;
    }

    public static boolean playAgain()
    {
        System.out.println("Play again?(y/n)");
        String input = sc.nextLine();
        if (input.toLowerCase().equals("y"))
        {
            return true;
        } else if (input.toLowerCase().equals("n"))
        {
            return false;
        } else
        {
            System.out.println("Invalid Input");
            return playAgain();
        }

    }

    public static String userChoice()
    {

        String user = "default";
        do
        {
            System.out.println("choose your weapon(Paper,Scissors or Rock)");
            user = sc.nextLine();
        } while (!isValidChoice(user));
        return user;
    }

    public static boolean isValidChoice(String choice)
    {
        boolean status;
        if (choice.equals("Rock"))
            status = true;
        else if (choice.equals("Paper"))
            status = true;
        else if (choice.equals("Scissors"))
            status = true;
        else
        {
            status = false;
            System.out.println("Error! Make sure you are capitalizing your choices");
        }

        return status;
    }

    public static void determineWinner(String computer, String user)
    {
        gameCount++;
        System.out.println(" Computer Choice: " + computer);
        System.out.println("Your Choice : " + user);
        if (computer.equals("Rock") && user.equals("Scissors"))
        {
            score--;
            System.out.println(" Computer wins! Better luck next time!");
        }
        if (computer.equals("Scissors") && user.equals("Paper"))
        {
            score--;
            System.out.println(" Computer wins! Better luck next time!");
        }
        if (computer.equals("Paper") && user.equals("Rock"))
        {
            score--;
            System.out.println(" Computer wins! Better luck next time!");
        }
        if (computer.equals("Rock") && user.equals("Paper"))
        {
            score++;
            System.out.println(" You win!!");
        }
        if (computer.equals("Scissors") && user.equals("Rock"))
        {
            score++;
            System.out.println(" You win!!");
        }
        if (computer.equals("Paper") && user.equals("Scissors"))
        {
            score++;
            System.out.println(" You win!!");
        } else if (computer.equals(user))
        {
            System.out.println(" Tie! the game must be played again.");
        }
        System.out.println("We have matched wits" + gameCount + "times, and your score is" + score);
        return;
    }

}