java語言訓練小遊戲,剪刀石頭布
package game; import java.util.Random; import java.util.Scanner;
//猜拳小遊戲 public class Caiquan { public static void main(String[] args) { System.out.println("==猜拳小遊戲="); String jixu = “Y”; Scanner in = new Scanner(System.in); while (jixu.equals(“Y”)) { System.out.println(“請出拳:1石頭 ,2剪刀,3布”); int personNumber = in.nextInt(); if (personNumber > 3) { System.out.println(“你出拳不對!!!”); } else { Random aa = new Random(); int computerNumber = aa.nextInt(3) + 1; String person = “”; String computer = “”; switch (personNumber) { case 1: person = “石頭”; break; case 2: person = “剪刀”; break; case 3: person = “布”; break; } switch (computerNumber) { case 1: computer = “石頭”; break; case 2: computer = “剪刀”; break; case 3: computer = “布”; break; } if (personNumber == 1 && computerNumber == 2 || personNumber == 2 && computerNumber == 3 || personNumber == 3 && computerNumber == 1) {
System.out.println("你出的是:" + person + ";電腦出的是:" + computer + ";你贏了!!!"); } else if (personNumber == computerNumber) { System.out.println("你出的是:" + person + ";電腦出的是:" + computer + ";平局了!!!"); } else { System.out.println("你出的是:" + person + ";電腦出的是:" + computer + ";你輸了!!!"); } System.out.println(); System.out.println("是否還要繼續:Y繼續,任意鍵退出"); jixu = in.next(); } } in.close(); System.out.println("歡迎下次光臨!!!"); }
}