Java 第十三章 人機猜拳
阿新 • • 發佈:2019-02-20
package com.kk; import java.util.*; public class out { String name; int Score; public int showout() { System.out.println("請出拳:1.剪刀 2.石頭 3.布(請輸入相應數字):"); Scanner input = new Scanner(System.in); int g = input.nextInt(); switch (g) { case 1: System.out.println("你出拳:剪刀"); break; case 2: System.out.println("你出拳:石頭"); break; case 3: System.out.println("你出拳:布"); break; } return g; } }
package com.kk; public class out11 { String name; int Score; out11 out; public int showout11() { int random = (int) (Math.random() * 3 + 1); switch (random) { case 1: System.out.println("電腦出拳:剪刀"); break; case 2: System.out.println("電腦出拳:石頭"); break; case 3: System.out.println("電腦出拳:布"); break; } return random; } }
package com.kk; import java.util.Scanner; public class out12 { out jia; out11 yi; int count; public void initial() { jia = new out(); yi = new out11(); count = 0; } public void showout12() { initial(); System.out.println("----------歡迎進入遊戲世界-----------"); System.out.println("**********************************"); System.out.println("*** 猜拳,開始 ***"); System.out.println("**********************************"); System.out.println("出拳規則:1.剪刀 2.石頭 3.布"); System.out.println("請選擇對方角色(1:劉備 2:孫權 3:曹操):"); Scanner input = new Scanner(System.in); int h = input.nextInt(); switch (h) { case 1: yi.name = "劉備"; break; case 2: yi.name = "孫權"; break; case 3: yi.name = "曹操"; break; } System.out.println("請輸入你的名字:"); jia.name = input.next(); System.out.println("你選擇了" + yi.name + "對戰"); System.out.println("要開始嗎?y:是 n:否"); String con = input.next(); while (con.equals("y")) { int per; int com; if (con.equals("y")) { per = jia.showout(); com = yi.showout11(); if ((per == 1 && com == 1) || (per == 2 && com == 2) || (per == 3 && com == 3)) { System.out.println("結果:和局,真衰!"); } else if ((per == 1 && com == 3) || (per == 2 && com == 1) || (per == 3 && com == 2)) { System.out.println("結果:恭喜,你贏了!"); jia.Score++; } else { System.out.println("結果說:^_^,你輸了,真笨!"); yi.Score++; } count++; } System.out.println("是否進行下一輪.y:是 n:否"); con = input.next(); } showgg(); } public void showgg() { System.out.println("-----------------------------"); System.out.println(jia.name + " VS " + yi.name); System.out.println("對戰次數:" + count); System.out.println("姓名" + "\t" + "積分"); System.out.println(jia.name + "\t" + jia.Score); System.out.println(yi.name + "\t" + yi.Score); if (jia.Score > yi.Score) { System.out.println("甲贏了"); } else if (jia.Score < yi.Score) { System.out.println("乙贏了"); System.out.println("-----------------------------"); } } }
package com.kk;
import java.util.*;
public class games {
public static void main(String[] args) {
out out = new out();
out12 out12 = new out12();
out12.showout12();
out11 out11 = new out11();
}
}