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

来源:互联网 发布:网络管理与通信软件 编辑:程序博客网 时间:2024/05/16 06:21
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();
}
}
0 0
原创粉丝点击