Java实现人机猜拳游戏

先写一个用户类:

import java.util.Scanner;

public class User {
    String uname;
    int uscore;
    int choice;

    public int showFist() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("\n请出拳:1.剪刀  2.石头    3.布");
        System.out.print("请输入你的选择:");
        choice = scanner.nextInt();
        switch (choice) {
            case 1:
                System.out.println("你出拳:剪刀");
                break;
            case 2:
                System.out.println("你出拳:石头");
                break;
            case 3:
                System.out.println("你出拳:布");
                break;
            default:
                System.out.println("输入有误!");
                break;
        }
        return choice;
    }

}

再写一个计算机类

public class Computer {
    String cname;
    int cscore;
    int choice;

    public int showFist() {
        choice = (int) (Math.random() * 10 % 3 + 1);
        switch (choice) {
            case 1:
                    System.out.println("电脑出拳:剪刀");
                break;
            case 2:
                    System.out.println("电脑出拳:石头");
                break;
            case 3:
                    System.out.println("电脑出拳:布");
                break;
            default:
                System.out.println("输入有误!");
                break;
        }
        return choice;
    }
}

最后写实现的游戏类

import java.util.Scanner;

public class Game {
    public static void main(String[] args) {
        User user = new User();
        Computer computer = new Computer();
        int count = 0;

        System.out.println("----------------欢迎进入游戏世界----------------");
        System.out.println();
        System.out.println();
        System.out.println("                ******************");
        System.out.println("                **  猜拳,开始  ** ");
        System.out.println("                ******************");
        System.out.println("出拳规则:1.剪刀 2.石头 3.布");
        System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操):");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        computer.cname = "";
        switch (choice) {
            case 1:
                computer.cname = "刘备";
                //System.out.println("你选择了刘备对战");
                break;
            case 2:
                computer.cname = "孙权";
                //System.out.println("你选择了孙权对战");
                break;
            case 3:
                computer.cname = "曹操";
                // System.out.println("你选择了曹操对战");
                break;
            default:
                System.out.println("输入有误!");
                break;
        }
        System.out.print("请输入你的姓名:");
        user.uname = scanner.next();
        System.out.println(user.uname + " VS" + " " + computer.cname + "对战");

        System.out.print("要开始吗?(y/n)");
        String input = scanner.next();
        while (input.equals("y")) {
            user.showFist();
            computer.showFist();

            int pc = user.choice - computer.choice;
            if (pc == 0) {
                System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
            } 
else if (pc == 1 || pc == -2) {
    computer.cscore++;
    System.out.println("结果:恭喜,你赢了!");
} else {
    user.uscore++;
    System.out.println("结果:^_^,你输了,真笨!");
}

            count++;

            System.out.print("是否开始下一轮?(y/n):");
            input = scanner.next();
            if (input.equals("n")) {
                break;
            }
        }
        System.out.println("------------------------------------------------");
        System.out.println(user.uname + " VS" + " " + computer.cname + "对战");
        System.out.println("对战次数:" + count);
        System.out.println();
        System.out.println("姓名" + "    " + "得分");
        System.out.println(user.uname + "     " + user.uscore);
        System.out.println(computer.cname + "     " + computer.cscore);
        if (user.uscore > computer.cscore) {
            System.out.println("完胜!");
        } else if (user.uscore == computer.cscore) {
            System.out.println("以和为贵");
        } else {
            System.out.println("惜败!");
        }
    }
}

此代码显示的“电脑出拳”不可以改成自己选择的对战角色,将会不断的改进自己的代码,继续加油吧!

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值