编写一个人与计算机对拿火柴的游戏程序。用随机函数产生火柴数量(20-50)每次最多拿3,拿到最后一根为胜

来源:百度知道 编辑:UC知道 时间:2024/05/26 02:43:27
要求:游戏过程要显示火柴总数,选择谁先拿;每步要显示剩余火柴数量,以及计算机拿的数量,并提示用户输入拿多少;结束时显示谁是胜者。

import java.util.Scanner;

public class MatchGame {

private static Scanner scanner = new Scanner(System.in);;
private int total;
private Computer com;
private static int exit = 1;

public MatchGame(int from, int to, String level) {
if (from >= to) {
throw new IllegalArgumentException();
}
total = (int)( Math.random() * (to - from)) + from;
com = new Computer(level);
}

public void start() {
System.out.println("0 means endGame, 4 means restartGame!");
System.out.println("The number of matches is " + total);
System.out.println("~Start~");
System.out.println("----------------------------------------");
while (true) {
int u = scanner.nextInt();
if (u > 0 && u < 4) {
System.out.println("You entered " + u);
if (total - u <= 0) {
exit = 2;