常胜将军
package 城市之间的最短距离; import java.util.Scanner; public class Main { /** * 常胜将军的 * @param args */ static int computer,user,last; public static void main(String[] args) { int num; System.out.println("请先输入火柴的数量为:"); Scanner input = new Scanner(System.in); num = input.nextInt(); System.out.println("火柴的总量为%d:"+num); last = num; jiangjun(); } private static void jiangjun() { while(true) { System.out.printf("--目前还有火柴%d根--\n",last); System.out.println("用户去火柴数量:"); Scanner input = new Scanner(System.in); user = input.nextInt(); if(user < 1|| user > 4|| user > last) { System.out.println("你违规了,你取得火柴有问题!"); continue; } last = last-user;//剩余火柴数量 if(last == 0) { System.out.println("用户取了最后一个火柴,因此计算机赢了"); break; }else{ computer = 5-user;//计算机取火柴数量 last = last-computer; System.out.printf("计算机取火柴数量:%d\n",computer); if(last == 0) { System.out.println("计算机取了最后一根火柴,因此用户赢了"); break; } } } } }
原文:http://www.cnblogs.com/aicpcode/p/4202296.html