21根火柴游戏(保证电脑胜利)

本文探讨了在限定火柴数量(21根)的情况下,通过编程实现的人机交互游戏,玩家与AI如何根据策略抽取火柴以确保AI最终获胜。通过控制火柴抽取范围在1-4根之间,实现智能决策过程。

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

package day141018;


/**
 * 
 * 21根火柴,每次选择1-4根火柴(人先取,最后保证电脑胜利)
 * @author XWJ
 */


import java.util.Scanner;


public class Match21 {
public static void main(String[] args) {
Scanner N = new Scanner(System.in);
int total = 21, total2 = 21;
while (total > 1) {
System.out.print("请输入您要抽取的火柴数量:");
int hum = N.nextInt();
int com;
if (1 <= hum || hum <= 4) {
com = 5 - hum;
System.out.printf("电脑抽取的火柴数量为:%d\n", com);
total = total - com - hum;
total2 = total - hum;
System.out.printf("剩余火柴数量为:%d\n", total);


if (total2 == 1) {
System.out.printf("玩家胜利!");
} else if (total == 1) {
System.out.printf("庄家胜利!");
}
}
}
N.close();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值