java拿火柴小游戏
时间: 2024-01-02 22:22:39 浏览: 120
基于JAVA实现的控制台火柴棍游戏
以下是一个Java实现的拿火柴小游戏的示例代码:
```java
import java.util.Scanner;
public class MatchGame {
public static void main(String[] args) {
int matches = 21;
int currentPlayer = 1;
Scanner scanner = new Scanner(System.in);
while (matches > 0) {
System.out.println("当前火柴数量:" + matches);
System.out.println("玩家" + currentPlayer + "请取走1-4根火柴:");
int numMatches = scanner.nextInt();
if (numMatches < 1 || numMatches > 4 || numMatches > matches) {
System.out.println("无效的选项,请重新输入!");
continue;
}
matches -= numMatches;
if (matches == 0) {
System.out.println("玩家" + currentPlayer + "输了!");
break;
}
currentPlayer = (currentPlayer == 1) ? 2 : 1;
}
scanner.close();
}
}
```
阅读全文