人機猜拳 (玩家、電腦、遊戲、測試)四個類寫法
阿新 • • 發佈:2018-10-11
猜拳 har play system.in 寫法 code ext imp com
第一個類,玩家類:
import java.util.Scanner; /** * 創建玩家類 */ public class Person { Scanner input = new Scanner(System.in); /** * 定義玩家類的屬性 */ String name; int score; String action; int num; /** * 定義玩家類的猜拳方法 */ public void method() { System.out.println("\n請出拳:1.剪刀 2.石頭 3.布");boolean a = true; do { num = input.nextInt(); if (num == 1 || num == 2 || num == 3) { switch (num) { case 1: action = "剪刀"; break; case 2: action = "石頭";break; case 3: action = "布"; break; } a = false; System.out.println("你出拳:" + action); } else { System.out.println("輸入數字有誤,請重新輸入"); } } while (a); } }
第二個類,電腦類:
/** * 電腦類 */ public class Computer { /** * 定義電腦類的屬性 */ String name; int score; String action; int num; /** * 定義電腦類的猜拳方法 */ public void method(){ /** *隨機產生數字123 */ num=(int)((Math.random())*3)+1; switch(num){ case 1: action="剪刀"; break; case 2: action="石頭"; break; case 3: action="布"; break; } System.out.println(name+"出拳:"+action); } }
第三個類,遊戲類:
import java.util.Scanner; /** * 遊戲類 */ public class Game { Scanner input = new Scanner(System.in); /** * 定義遊戲類的屬性 */ Person person = new Person();//創建玩家類的對象 Computer computer = new Computer();//創建電腦類的對象 int number; int frequency = 0; /** * 定義遊戲進程的方法 */ public void process() { System.out .println("--------------------------歡迎進入遊戲世界--------------------------\n"); System.out.println("\t\t********************************"); System.out.println("\t\t**********猜拳,開始*************"); System.out.println("\t\t********************************"); System.out.println(); System.out.println("出拳規則:1.剪刀 2.石頭 3.布"); System.out.print("請選擇對方角色(1:劉備2:孫權3:曹操):"); boolean b = true; do { number = input.nextInt(); if (number == 1 || number == 2 || number == 3) { switch (number) { case 1: computer.name = "劉備"; break; case 2: computer.name = "孫權"; break; case 3: computer.name = "曹操"; break; } b = false; } else { System.out.println("輸入數字有誤,請重新輸入"); } } while (b); System.out.print("請輸入你的姓名:"); person.name = input.next(); System.out.println(person.name + " VS " + computer.name + " 對戰\n"); System.out.println("要開始嗎?(y/n)"); char answer = input.next().charAt(0); while (answer == ‘y‘) { person.method(); computer.method(); if (person.num == computer.num) { System.out.println("嘿嘿,和局,等著瞧吧!"); } else if (((person.num == 2) && (computer.num == 1)) || (person.num == 1) && (computer.num == 3) || ((person.num == 3) && (computer.num == 2))) { System.out.println("哇,你贏了,好厲害!"); person.score++; } else { System.out.println("^_^!!!你輸了,真笨!"); computer.score++; } frequency++; System.out.println("\n"); System.out.println("還要繼續嗎?(y/n)"); answer = input.next().charAt(0); } } /** * 定義遊戲結算的方法 */ public void showResult() { System.out.println("********************************"); System.out.println(person.name + " VS " + computer.name); System.out.println("對戰次數:" + frequency); System.out.println("\n姓名\t\t得分"); System.out.println(person.name + "\t\t" + person.score); System.out.println(computer.name + "\t\t" + computer.score); if (person.score < computer.score) { System.out.println("呵呵,笨笨,下次加油!"); } else if (person.score == computer.score) { System.out.println("哇,竟然平局,我們下次一決勝負!"); } else { System.out.println("哇,你好棒啊!"); } System.out.println("********************************"); } }
測試類:
public class Demo { public static void main(String[] args) { Game play=new Game();//創建遊戲類的對象 play.process();//調用遊戲類的遊戲進程方法 play.showResult();//調用遊戲類的結算方法 } }
人機猜拳 (玩家、電腦、遊戲、測試)四個類寫法