模拟谁最厉害小游戏

本文介绍了一个简单的火柴游戏的Java实现。游戏的目标是在玩家和电脑之间交替取走火柴,每次可以取1到3根,直至一方取到最后剩下的带有特殊标记的火柴即为胜利。文中展示了完整的Java代码,包括游戏逻辑的实现。

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

import java.util.*;
import java.io.*;


class Game {
Scanner scanner = new Scanner(System.in);
LinkedList save = new LinkedList();
int coptNum = 0, humanNum = 0;
Game() {
for(int i = 0; i < 20 + (Math.random() * 10 / 3); i++) {
save.add("b");
}
save.add("a");
}


public int getMachinNum() {
int compN = (int) (Math.random() * 10 / 3);
return compN;
}
public int getHumenNum() {
int humenN = new Scanner(System.in).nextInt();
return humenN;
}
}


public class stickGame {
static int n = 0;
public static void main(String args[]){
try{
Game game = new Game();
System.out.println("你要拿几个火柴?(1~3根):");
while(!(stickGame.n == 1 && stickGame.n == 2)) {
int n =  game.getHumenNum();
for(int i = 0; i <= n; i++) {
String string1 = (String) game.save.get(i);
if(string1 == "a"){
System.out.println("电脑赢");
break;
}
game.save.remove(0);
}
System.out.println("电脑拿了" + game.getMachinNum() + "个火柴");
for(int j = 0; j < game.getMachinNum(); j++) {
String string2 = (String)game.save.get(j);
if(string2 == "a") {
stickGame.n = 2;
System.out.println("玩家赢");
break;
}
game.save.get(0);
}
System.out.println("你要拿几个火柴?");
}
}catch(Exception e) {
System.out.println("游戏结束!");
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值