石头剪刀布游戏代码(Java)

import java.util.Scanner;

public class game1_modi2 {
    public static void main(String[] args) {
        String player_name;
        String com_name;
        int player_choice;
        int com_choice;
        String player_choice_name;
        String com_choice_name;
        System.out.println("**********************************");
        System.out.println("    游戏:剪刀石头布");
        System.out.println("                作者:XXXXXX");
        System.out.println("                版本:v1.2");
        System.out.println("*********************************");
        System.out.println("欢迎进入本游戏,请输出游戏玩家的名字:");
        Scanner scanner = new Scanner(System.in);
        player_name=scanner.nextLine();
        if(player_name.trim().equals("")){
            player_name="匿名玩家";
        }
        System.out.println("********************************");
        System.out.println("1-----蜡笔小新");
        System.out.println("2-----葫芦娃");
        System.out.println("*********************************");
        String com_no=scanner.next();
        switch(com_no){
            case "1":
                com_name="蜡笔小新";
                break;
            case "2":
                com_name="葫芦娃";
                break;
            default:
                com_name="匿名用户";
        }
        System.out.println("****请玩家选择出拳的代表数字*********");
        System.out.println("1------剪刀");
        System.out.println("2------石头");
        System.out.println("3------布");
        System.out.println("*********************************");
        String tmp_choice=scanner.next();
        switch(tmp_choice){
            case "1":
                player_choice=Integer.parseInt(tmp_choice);
                break;
            case "2":
                player_choice=Integer.parseInt(tmp_choice);
                break;
            case "3":
                player_choice=Integer.parseInt(tmp_choice);
                break;
            default:
                player_choice=(int)Math.round(Math.random()*2+1);

        }
        switch(player_choice){
            case 1:
                player_choice_name="剪刀";
                break;
            case 2:
                player_choice_name="石头";
                break;
            default:
                player_choice_name="布";

        }
        com_choice=(int)Math.round(Math.random()*2+1);
        switch(com_choice){
            case 1:
                com_choice_name="剪刀";
                break;
            case 2:
                com_choice_name="石头";
                break;
            default:
                com_choice_name="布";

        }
        if(player_choice-com_choice==1 || player_choice-com_choice==-2){
            System.out.println("玩家:"+player_name+",出拳:"+player_choice_name+"赢");
            System.out.println("电脑:"+com_name+",出拳:"+com_choice_name+"输");
        }else if(com_choice-player_choice==1 || com_choice-player_choice==-2){
            System.out.println("电脑:"+com_name+",出拳:"+com_choice_name+"赢");
            System.out.println("玩家:"+player_name+",出拳:"+player_choice_name+"输");
        }else{
            System.out.println("电脑:"+com_name+",出拳:"+com_choice_name+"平局");
            System.out.println("玩家:"+player_name+",出拳:"+player_choice_name+"平局");
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值