Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
石头剪刀布简单java使用方法_Java - Fatal编程技术网

石头剪刀布简单java使用方法

石头剪刀布简单java使用方法,java,Java,所以,我今天的任务是用各种方法制作一个石头剪刀游戏。我的代码的第一个问题是,我需要它来询问用户是否想要再次播放。(y或n)此外,我需要实施计分,如果用户赢了,得1分,如果他们输了,得1分,并且每次他们再次玩时,将该分数添加到总分中。关于如何实现这一点有什么想法吗?或者我需要更改代码的内容。我也是一个菜鸟,对难看的格式很抱歉,可以随意批评每一个小细节,我会吸收所有我能吸收的信息 public static void main(String[] args){ boolean tie = true

所以,我今天的任务是用各种方法制作一个石头剪刀游戏。我的代码的第一个问题是,我需要它来询问用户是否想要再次播放。(y或n)此外,我需要实施计分,如果用户赢了,得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

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

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

}

您知道如何反复请求“武器”,但不知道如何反复请求播放?使用.equals()可以更好地检查response@ScottHunter所以我会做另一个方法,让他们再玩一遍?你们在我的编程课上的第一节课,我们还没有讲到课中的角色,使用课堂,所以我不太明白它们是做什么的/它们是如何使用的。@FungucideIt与原始代码中的完全一样,它调用play()方法,而play()方法曾经是您的主要方法。然后它询问用户是否想再次使用playReach()方法,然后重复,直到用户说不。我格式化while循环的方式也不同。使用do{…}while(条件)。这种方式是while(condition){…},但本质上是相同的,我指的是这个类--public class RPC{public static Scanner sc=new Scanner(System.in);public static int score=0;public static int gameCount=0;我将变量公开,以便所有函数都可以访问它。这样,我就能够跟踪分数,而不必通过函数一致地传递分数。我还添加了类,因为我必须取消程序的运行以确保其正常运行。另外,您以前是否编译并运行过java程序?这很糟糕,因为您需要一个类来编译和运行java程序。另外,请阅读以下内容