java 一个简单的猜拳小游戏

这是一个使用Java编写的猜拳游戏程序,包含一局定胜负、三局两胜和五局三胜三种玩法。玩家可以输入石头、剪刀或布,与计算机进行对决,游戏结束后可以选择是否继续。程序中包含了生成随机数、玩家输入判断、游戏结果判定等核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.Random;
import java.util.Scanner;

public class CaiQuanYouXi {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(“欢迎来到南筏の猜拳游戏!”);
String next = “是”;
int flag = 1;
while (next.equals(“是”)) {
System.out.println(“是否要了解游戏规则呢?”);
System.out.println(“请输入 是/否”);
String str = sc.next();
while (!str.equals(“是”) & !str.equals(“否”)) {
System.out.println(“您的输入有误!请输入是/否”);
str = sc.next();
}
if (str.equals(“是”)) {
System.out.println(“本次猜拳游戏有三种玩法”);
System.out.println(“输入1:采取一局定胜负”);
System.out.println(“输入2:采取三局两胜制”);
System.out.println(“输入3:采取五局三胜制”);
}
System.out.print(“*******请选择您想要的玩法:”);
int wanfa = sc.nextInt();
while (wanfa != 1 & wanfa != 2 & wanfa != 3) {
System.out.print(“您的输入有误!请输入1或2或3:”);
wanfa = sc.nextInt();
}
switch (wanfa) {
case 1:
bo1(flag);
break;
case 2:
bo3(flag);
break;
case 3:
bo5(flag);
break;
}
System.out.println(“还要继续吗?”);
System.out.println(“输入是:继续游戏”);
System.out.println(“输入否:结束游戏”);
next = sc.next();
if (!next.equals(“是”) & !next.equals(“否”)) {
System.out.print(“您的输入有误!请输入是/否:”);
next = sc.next();
}
flag = 0;
}
}

//生成随机的1、2、3的方法
public static int Shengcheng() {
    Random random = new Random();
    int a = random.nextInt(3) + 1;//生成随机数(1或2或3)
    return a;
}

//玩家输入的方法
public static int wanjiashutu(int flag, int i, int bo) {
    Scanner sc = new Scanner(System.in);
    if (flag == 1) {
        System.out.println("第" + i + "/" + bo + "局");
        System.out.println("*******请选择您将要出的种类:*******");
        System.out.println("*******输入1:代表您出的是石头*******");
        System.out.println("*******输入2:代表您出的是剪刀*******");
        System.out.println("*******输入3:代表您出的是布*******");
    } else {
        System.out.println("第" + i + "/" + bo + "局");
        System.out.print("*******请选择您将要出的种类(1石头2剪刀3布):");
    }
    int type = sc.nextInt();
    while (type != 1 & type != 2 & type != 3) {
        System.out.print("您的输入有误!请输入1或2或3:");
        type = sc.nextInt();
    }
    return type;
}

//判断游戏结果的方法
public static void Jieguo(int a, int b) {
    if (a == 1) {
        if (b == 1) {
            System.out.println("*******本局游戏结果为:您和南筏都出的石头!*******");
            System.out.println("*******恭喜您,这一局和南筏打平了!*******");
        }
        if (b == 2) {
            System.out.println("*******本局游戏结果为:您出的石头,而南筏出的剪刀!*******");
            System.out.println("*******恭喜您,这一局战胜了南筏!*******");
        }
        if (b == 3) {
            System.out.println("*******本局游戏结果为:您出的石头,而南筏出的布!*******");
            System.out.println("*******很遗憾,这一局南筏击败了你!*******");
        }
    }
    if (a == 2) {
        if (b == 1) {
            System.out.println("*******本局游戏结果为:您出的剪刀,而南筏出的石头!*******");
            System.out.println("*******很遗憾,这一局南筏击败了你!*******");
        }
        if (b == 2) {
            System.out.println("*******本局游戏结果为:您和南筏都出的剪刀!*******");
            System.out.println("*******恭喜您,这一局和南筏打平了!*******");
        }
        if (b == 3) {
            System.out.println("*******本局游戏结果为:您出的剪刀,而南筏出的布!*******");
            System.out.println("*******恭喜您,这一局战胜了南筏!*******");
        }
    }
    if (a == 3) {
        if (b == 1) {
            System.out.println("*******本局游戏结果为:您出的布,而南筏出的石头!*******");
            System.out.println("*******恭喜您,这一局战胜了南筏!*******");
        }
        if (b == 2) {
            System.out.println("*******本局游戏结果为:您出的布,而南筏出的剪刀!*******");
            System.out.println("*******很遗憾,这一局南筏击败了你!*******");
        }
        if (b == 3) {
            System.out.println("*******本局游戏结果为:您和南筏都出的布!*******");
            System.out.println("*******恭喜您,这一局和南筏打平了!*******");
        }
    }
}

//判断胜负场数
public static int Shengfu(int a, int b) {
    if (a == b) {
        return 0;
    } else if ((a == 1 && b == 2) || (a == 2 && b == 3) || (a == 3 && b == 1)) {
        return 1;
    } else {
        return 2;
    }

}

//一局定胜负方法
public static void bo1(int flag) {
    int wanjia = wanjiashutu(flag, 1, 1);
    int diannao = Shengcheng();
    Jieguo(wanjia, diannao);
}

//三局两胜制方法
public static void bo3(int flag) {
    int a[] = new int[3];
    for (int i = 0; i < 3; i++) {
        int wanjia = wanjiashutu(flag, i + 1, 3);
        int diannao = Shengcheng();
        switch (Shengfu(wanjia, diannao)) {
            case 0:
                a[0]++;
                break;
            case 1:
                a[1]++;
                break;
            case 2:
                a[2]++;
                break;
        }
        Jieguo(wanjia, diannao);
        if (a[1] == 2 || a[2] == 2) {
            break;
        }
    }
    if (a[1] == a[2]) {
        System.out.println("*******在三局两胜制中,您最终与南筏打平了!*******");
    } else if (a[1] > a[2]) {
        System.out.println("*******在三局两胜制中,您最终战胜了南筏!*******");
    } else if (a[1] < a[2]) {
        System.out.println("*******在三局两胜制中,您最终输给了南筏!*******");
        System.out.println("是不是不服气!");
    }
}

//五局三胜制方法
public static void bo5(int flag) {
    int a[] = new int[5];
    for (int i = 0; i < 5; i++) {
        int wanjia = wanjiashutu(flag, i + 1, 5);
        int diannao = Shengcheng();
        switch (Shengfu(wanjia, diannao)) {
            case 0:
                a[0]++;
                break;
            case 1:
                a[1]++;
                break;
            case 2:
                a[2]++;
                break;
        }//记录每场对局中的结果次数
        Jieguo(wanjia, diannao);
        if (a[1] == 3 || a[2] == 3) {
            break;
        }
    }
    if (a[1] == a[2]) {
        System.out.println("*******在五局三胜制中,您最终与南筏打平了!*******");
    } else if (a[1] > a[2]) {
        System.out.println("*******在五局三胜制中,您最终战胜了南筏!*******");
    } else if (a[1] < a[2]) {
        System.out.println("*******在五局三胜制中,您最终输给了南筏!*******");
        System.out.println("是不是不服气!");
    }
}

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值