因为采用了随机数,因此每次编译结果都不同,结果我就不附图了。java
public class Elephant { /* * 大象工程 * 1.打开冰箱门 * 2.大象PK * 3.把赢的大象放进冰箱 * 4.关闭冰箱门 * */ public static void main(String[] args) { System.out.println("大象工程启动:"); //准备两头大象,一台冰箱 String elephantA = "大象A"; String elephantB = "大象B"; //打开冰箱门 System.out.println(" 1.打开冰箱门"); //大象pk //石头剪刀布,石头1>剪刀2>布3>石头1,用数字代替字符 //采用Math函数的Math.random()产生3之内的随机数,(int)(Math.random()*3+1) System.out.println(" 2.大象pk"); //采用do.while循环,当出现平局时能够继续pk String winElephant = null; int i = 0; do { i++; int elephantNumA = (int) (Math.random() * 3 + 1); int elephantNumB = (int) (Math.random() * 3 + 1); //pk开始 //采用switch将数字和字符对应起来 String elephantNameA = null; String elephantNameB = null; switch (elephantNumA) { case 1: elephantNameA = "石头"; break; case 2: elephantNameA = "剪刀"; break; case 3: elephantNameA = "布"; break; default: System.out.println("错误!"); break; } switch (elephantNumB) { case 1: elephantNameB = "石头"; break; case 2: elephantNameB = "剪刀"; break; case 3: elephantNameB = "布"; break; default: System.out.println("错误!"); break; } System.out.println(" "+"第"+i+"场"+" "+elephantA+"-"+elephantNameA+" PK "+elephantB+"-"+elephantNameB); //三种状况:A赢,B赢,平局 if (elephantNumA == 1 & elephantNumB == 2 || elephantNumA == 2 & elephantNumB == 3 || elephantNumA == 3 & elephantNumB == 1) { System.out.println(" "+elephantA + "赢了"); winElephant = elephantA; } else if (elephantNumA == elephantNumB) { System.out.println(" "+"平局"); } else { System.out.println(" "+elephantB + "赢了"); winElephant = elephantB; } } while (winElephant == null); //胜利的进入冰箱 System.out.println(" 3.把"+winElephant+"放进冰箱"); //关闭冰箱门,结束 System.out.println(" 4.关闭冰箱门"); System.out.println("大象工程结束!"); } }