要求用户输入一个0到2之间的整数,如果用户输入0输出“你出的是石头”, 如果用户输入1就输出“你出的是剪刀”,如果用户输入的是2就输出“你出的是布”, 然后再问是否要继续出拳,

    /*2.要求用户输入一个0到2之间的整数,如果用户输入0输出“你出的是石头”,
      如果用户输入1就输出“你出的是剪刀”,如果用户输入的是2就输出“你出的是布”,
      然后再问是否要继续出拳,如果回答“y”就重复以上过程,否则结束程序。

       */

import java.util.Scanner;

public class Demo02 {
    //定义猜拳方法
    public static void game(int number) {
        if (number == 0) {
            System.out.println("你出的是石头");

        } else if (number == 1) {
            System.out.println("你出的是剪刀");
        } else if (number == 2) {
            System.out.println("你出的是布");

        } else {
            System.out.println("输入错误");

        }


    }

    public static void main(String[] args) {

        System.out.println("请输入0到2之间的整数");
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        game(number);

        //第二轮游戏
        System.out.println("继续请输入y:");
        Scanner sc1 = new Scanner(System.in);
        String agin = sc1.nextLine();
        if (agin.equals("y")) {
            System.out.println("请输入0到2之间的整数");
            int number1 = sc.nextInt();
            game(number1);
        } else {
            System.out.println("游戏结束");
        }

    }


}

方法二:

import java.util.Scanner;

public class Demo02_2 {
    public static void main(String[] args) {
        System.out.println("请输入0到2之间的整数");
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        switch (number) {
            case 0:
                System.out.println("你出的是石头");
                break;
            case 1:
                System.out.println("你出的是剪刀");
                break;
            case 2:
                System.out.println("你出的是布");
                break;
        }
        //第一次游戏结束
        System.out.println("继续请输入y:");
        Scanner sc1 = new Scanner(System.in);
        String agin = sc1.nextLine();
        if (agin.equals("y")) {         //判断输入的值是否是y
            System.out.println("请输入0到2之间的整数");
            int number1 = sc.nextInt();
            switch (number1) {
                case 0:
                    System.out.println("你出的是石头");
                    break;
                case 1:
                    System.out.println("你出的是剪刀");
                    break;
                case 2:
                    System.out.println("你出的是布");
                    break;
            }
            System.out.println("游戏结束");
        } else {
            System.out.println("游戏结束");
        }
    }
}

 方法三

import java.util.Scanner;

public class Demo02_1 {
    //定义方法
    public static  void game(int number){
        switch (number){
            case 0:
                System.out.println("你出的是石头"); break;
            case 1:
                System.out.println("你出的是剪刀");break;
            case 2:
                System.out.println("你出的是布");break;
        }
    }
    public static void main(String[] args) {
        System.out.println("请输入0到2之间的整数");
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        game(number);

        //第二轮游戏
        System.out.println("继续请输入y:");
        Scanner sc1 = new Scanner(System.in);
        String agin = sc1.nextLine();
        if (agin.equals("y")) {
            System.out.println("请输入0到2之间的整数");
            int number1 = sc.nextInt();
            game(number1);
        } else {
            System.out.println("游戏结束");
        }


    }
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 以下是Python代码,可以实现您的要求: ```python import random # 获取用户输入 choice = int(input("请选择:0表示剪刀,1表示石头,2表示:")) # 0表示剪刀,1表示石头,2表示 if choice == 0: user_choice = "剪刀" elif choice == 1: user_choice = "石头" elif choice == 2: user_choice = "" else: print("输入错误,请重新输入!") exit() # 随机生成电脑选择 computer_choice = random.randint(0, 2) if computer_choice == 0: computer_choice = "剪刀" elif computer_choice == 1: computer_choice = "石头" else: computer_choice = "" # 打印双方选择 print("你选择了:%s,电脑选择了:%s。" % (user_choice, computer_choice)) # 判断胜负 if choice == computer_choice: print("平局!") elif choice == 0 and computer_choice == 1: print("你输了!") elif choice == 1 and computer_choice == 2: print("你输了!") elif choice == 2 and computer_choice == 0: print("你输了!") else: print("你赢了!") ``` 在这个代码中,我们使用了Python的`random`模块来生成随机数。我们还使用了条件语句来判断用户输入的选项,并且用字符串变量来存储剪刀石头的文字表示。最后,我们使用了一系列的条件语句来判断胜负,并打印结果。 ### 回答2: 下面是一个用Python编写的石头剪刀游戏的例子,按照要求实现了用户输入和电脑随机生成数字,并比较它们的出拳情况,输出赢、输和平局的结果。 ```python import random def get_user_choice(): while True: user_input = input("请输入你的选择(0表示剪刀,1表示石头,2表示):") if user_input in ['0', '1', '2']: return int(user_input) else: print("输入无效,请重新输入!") def get_computer_choice(): return random.randint(0, 2) def compare_choices(user_choice, computer_choice): if user_choice == computer_choice: return "平局" elif user_choice - computer_choice == 1 or user_choice - computer_choice == -2: return "赢" else: return "输" def play_game(): user_choice = get_user_choice() computer_choice = get_computer_choice() choices = ["剪刀", "石头", ""] print("你了", choices[user_choice]) print("电脑了", choices[computer_choice]) result = compare_choices(user_choice, computer_choice) if result == "平局": print("平局!") elif result == "赢": print("你赢了!") else: print("你输了!") play_game() ``` 请注意,以上代码只是一个简单的实现示例,可能还有改进的余地。例如,可以添加输入的验证机制,确保用户输入的是有效的整数。 ### 回答3: 下面是一个用Python编写的石头剪刀的游戏的示例代码: ```python import random def rock_paper_scissors(player_choice): options = ["剪刀", "石头", ""] computer_choice = random.randint(0, 2) print("你了:" + options[player_choice]) print("电脑了:" + options[computer_choice]) if player_choice == computer_choice: print("平局") elif (player_choice == 0 and computer_choice == 2) or \ (player_choice == 1 and computer_choice == 0) or \ (player_choice == 2 and computer_choice == 1): print("你赢了") else: print("你输了") # 获取用户输入 player_choice = int(input("请输入剪刀(0),石头(1)或(2):")) # 校验用户输入 if player_choice not in [0, 1, 2]: print("输入有误,请输入0、1或2") else: rock_paper_scissors(player_choice) ``` 这段代码中,我们首先使用`import random`导入random模块,以便能够生成电脑的随机选择。然后定义了一个名为`rock_paper_scissors`的函数,接受玩家的选择作为参数。 在主程序中,我们通过`input`函数获取用户输入,并使用`int(input())`将其转换为整数类型。然后使用条件语句对用户输入进行校验。如果玩家输入的不是0、1或2,则输出错误消息。否则,调用`rock_paper_scissors`函数,并将玩家的选择作为参数传递给它。 在`rock_paper_scissors`函数中,我们首先定义了一个`options`列表,其中包含了对应每个选项的字符串。然后使用`random.randint(0, 2)`生成一个随机数作为电脑的选择。接下来,根据玩家和电脑的选择进行比较,根据比较结果输出相应的信息。如果玩家和电脑的选择相同,则输出平局;否则,根据规则判断胜负。 希望这样的回答能对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Royalreairman

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值