package com.lovo;
import java.util.Scanner;
/**
* 21根火柴的游戏
* 两个人分别拿火柴,每人每次只能1~4根火柴,拿到最后一根火柴的人就输了
* @author 李睿
*
*/
public class Text02 {
public static void main(String[] args) {
int lest = 21;
Scanner sc = new Scanner(System.in);
do {
System.out.print("需要拿走几根火柴:");
do {
if (sc.hasNextInt()) {
int take1 = sc.nextInt();
do {
if (take1 > 0 && take1 < 5) {
int take2 = 5 - take1;
lest = lest - take1 - take2;
if (lest > 1) {
System.out.println("电脑拿走" + take2 + "根火柴");
System.out.println("剩下" + lest + "根火柴");
System.out.print("需要拿走几根火柴:");
break;
} else {
System.out.println("电脑拿走" + take2 + "根火柴");
System.out.println("剩下" + lest + "根火柴");
System.out.println("你输了!");
break;
}
} else {
System.out.print("输入错误!!");
System.out.print("需要拿走几根火柴:");
break;
}
} while (take1 > 0 && take1 < 5);
} else {
System.out.print("输入错误!!");
System.out.print("需要拿走几根火柴:");
break;
}
} while (sc.hasNextInt());
} while (lest >= 1);
sc.close();
}
}
21根火柴游戏
最新推荐文章于 2023-03-31 14:22:02 发布