我如何用Java做石头剪刀? [英] How do I do rock paper scissors in Java?

查看:65
本文介绍了我如何用Java做石头剪刀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还记得儿时的游戏摇滚,纸,剪刀吗?这是一个双人游戏,每个人同时选择摇滚,纸张或剪刀。岩石击败剪刀但是失去了纸张,纸张击败了岩石,但却失去了剪刀,剪刀击败了纸张,但却失去了摇滚乐。以下代码提示玩家1和玩家2各自输入一个字符串:摇滚,纸张或剪刀。通过添加嵌套的if语句来完成代码,以适当地报告Player 1 wins,Player 2 wins或这是一个平局。



到目前为止这个是我唯一拥有的东西,在此之后我无能为力......如果它们不相等,我如何比较字符串?

Remember the childhood game "Rock, Paper, Scissors"? It is a two-player game in which each person simultaneously chooses either rock, paper, or scissors. Rock beats scissors but loses to paper, paper beats rock but loses to scissors, and scissors beats paper but loses to rock. The following code prompts player 1 and player 2 to each enter a string: rock, paper, or scissors. Finish the code by adding nested if statements to appropriately report "Player 1 wins", "Player 2 wins", or "It is a tie."

so far this is the only thing i have, I'm clueless after this point...how do i compare strings if they aren't equal??

import java.util.Scanner;
public class RockPaperScissors
{
     public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Player 1: Choose rock, scissors, or paper:";);
        String player1 = scan.next().toLowerCase();
        System.out.println("Player 2: Choose rock, scissors, or paper:");
        String player2 = scan.next().toLowerCase();

          if (player1.equals(player2))
          {
            System.out.print("It is a tie");
          }
        .....
    }
}

推荐答案

我认为一个更好的方法是将选项视为整数,它更容易比较。

我会做这样的事情:



I think a better aproach would be to treat the options as integers, it's easier to compare.
I would do something like this:

final static int ROCK = 1, SCISSOR = 2, PAPER = 3;
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Player 1: Choose (1) - Rock, (2) - Scissors, or (3) - Paper: ");
        int player1 = scan.nextInt();
        System.out.println("Player 2: Choose (1) - Rock, (2) - Scissors, or (3) - Paper: ");
        int player2 = scan.nextInt();
     
        if (player1 == player2)
        {
            System.out.print("It is a tie");
        } else {
            switch (player1){
            case ROCK:
                if (player2 == SCISSOR)
                    System.out.print("Player 1 wins!");
                else
                    System.out.print("Player 2 wins!");
                break;
            case SCISSOR:
                if (player2 == PAPER)
                    System.out.print("Player 1 wins!");
                else
                    System.out.print("Player 2 wins!");
                break;
            case PAPER:
                if (player2 == ROCK)
                    System.out.print("Player 1 wins!");
                else
                    System.out.print("Player 2 wins!");
                break;
            }
        }
    }


这篇关于我如何用Java做石头剪刀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆