diff --git "a/\345\245\275\347\216\251\344\275\206\346\262\241\344\273\200\344\271\210\347\224\250\344\273\243\347\240\201/Java-\347\237\263\345\244\264\345\211\252\345\255\220\345\270\203\345\260\217\346\270\270\346\210\217" "b/\345\245\275\347\216\251\344\275\206\346\262\241\344\273\200\344\271\210\347\224\250\344\273\243\347\240\201/Java-\347\237\263\345\244\264\345\211\252\345\255\220\345\270\203\345\260\217\346\270\270\346\210\217" new file mode 100644 index 0000000000000000000000000000000000000000..d1bf874b388dc7e971b2998d67b8fdc209fd4ed4 --- /dev/null +++ "b/\345\245\275\347\216\251\344\275\206\346\262\241\344\273\200\344\271\210\347\224\250\344\273\243\347\240\201/Java-\347\237\263\345\244\264\345\211\252\345\255\220\345\270\203\345\260\217\346\270\270\346\210\217" @@ -0,0 +1,51 @@ +import java.util.Random; +import java.util.Scanner; +import java.util.concurrent.TimeUnit; + +/** + * 石头剪子布游戏:✌️、✊、🤚 + * 当两人意见出现分歧时,玩石头剪子布的游戏可以帮助做出决策 + * 程序的用法:按下回车出结果 + */ +public class RockPaperScissors { + static boolean flag = true; + + public static void main(String[] args) throws InterruptedException { + + Scanner sc = new Scanner(System.in); + //按回车键终止程序 + new Thread(() -> { + sc.nextLine(); + flag = false; + }).start(); + + //下面这段程序的作用是动态展示A与B的出手 + String[] img = {"✌️", "✊", "🤚"}; + Random random = new Random(); + int a = random.nextInt(3); + int b = random.nextInt(3); + while (flag) { + System.out.print(img[a] + " VS " + img[b] + "\r"); // \r的作用是光标回到行首 + TimeUnit.MILLISECONDS.sleep(10); + a = random.nextInt(3); + b = random.nextInt(3); + } + System.out.println("=================================================="); + System.out.println("[A: " + img[a] + "] VS [" + "B: " + img[b] + "]"); //最终结果 + + /* + * 判断A和B的胜负关系:✌️=0,✊=1,🤚=2,三者是一个循环的关系,即 0 < 1 < 2 < 0 + * 如果B在A左边,那么A获胜 + * 如果B在A右边,那么B获胜 + * 否则是平局 + * */ + if ((a + 2) % 3 == b) { // B在A左边 + System.out.println("A win!"); + } else if ((a + 1) % 3 == b) { // B在A右边 + System.out.println("B win!"); + } else { + System.out.println("平局!"); + } + + } +} \ No newline at end of file