使用java實現猜拳小遊戲
本文實例為大傢分享瞭java實現猜拳小遊戲的具體代碼,供大傢參考,具體內容如下
實現下圖要求
public class User { private String u_name; private int u_score; public User() { super(); } public User(String name, int score) { super(); this.u_name = name; this.u_score = score; } public String getName() { return u_name; } public void setName(String name) { this.u_name = name; } public int getScore() { return u_score; } public void setScore(int score) { this.u_score = score; } /** * 出拳方法 * @param choice 選擇的數字代表出拳(1:石頭2:剪刀3:佈) * @return str 返回你所選擇的出拳 */ public String chuQuan(int choice){ String str = ""; switch (choice) { case 1: str = "石頭"; break; case 2: str = "剪刀"; break; case 3: str = "佈"; break; default: System.out.println("未知錯誤"); break; } return str; } }
public class Computer { private String c_name; private int c_score; public String getName() { return c_name; } public void setName(String name) { this.c_name = name; } public int getScore() { return c_score; } public void setScore(int score) { this.c_score = score; } /** * 出拳方法 * @param choice 選擇的數字代表出拳(1:石頭2:剪刀3:佈) * @return str 返回你所選擇的出拳 */ public String chuQuan(int choice){ String str = ""; switch (choice) { case 1: str = "石頭"; break; case 2: str = "剪刀"; break; case 3: str = "佈"; break; default: System.out.println("未知錯誤"); break; } return str; } }
import java.util.Scanner; public class Game { Scanner input = new Scanner(System.in); private User user; private Computer computer; private int count; private int c_score; private int u_score; //初始化方法 public void init(){ user = new User(); computer = new Computer(); System.out.println("-----------------歡迎進入遊戲世界------------------"); System.out.println("\t **************************"); System.out.println("\t\t** 猜拳,開始 **"); System.out.println("\t **************************"); System.out.println(); System.out.println("出拳規則:1.石頭 2.剪刀 3.佈"); System.out.print("請選擇對方角色:(1:曹操 2:孫權 3:劉備):"); int key = input.nextInt(); switch (key) { case 1: computer.setName("曹操"); break; case 2: computer.setName("孫權"); break; case 3: computer.setName("劉備"); break; default: System.out.println("非法輸入..."); break; } System.out.print("請輸入你的姓名:"); user.setName(input.next()); System.out.println(user.getName()+" VS "+computer.getName()); begin(); } //是否開始執行 循環執行直到輸入n結束 public void begin(){ System.out.print("要開始嗎(y/n):"); // boolean falg = true; String str = input.next(); if(str.equals("y")){ while(true){ score(); System.out.print("是否開始下一輪:(y/n)"); String str1 = input.next(); count++; if(str1.equals("y")){ }else{ // falg = false; break; } } } show(); } //人和機器出拳並判斷勝負 此處計算比賽次數 雙方得分 public void score(){ System.out.print("請出拳:"); int choice1 = input.nextInt(); String str1 = user.chuQuan(choice1); int choice2 = (int)(Math.random()*3+1); String str2 = computer.chuQuan(choice2); System.out.println("你出拳"+str1); System.out.println(computer.getName()+"出拳"+str2); if(choice1 == choice2){ System.out.println("結果:平局"); }else if(choice2-choice1==-1||choice2-choice1==2){ System.out.println("結果:"+computer.getName()+"獲勝..."); c_score++; computer.setScore(c_score); }else if(choice1-choice2==-1||choice1-choice2==2){ System.out.println("結果:恭喜你,你獲勝..."); u_score++; user.setScore(u_score); } } //顯示比賽結果並比較得得出最後勝負 public void show(){ System.out.println("--------------------------------"); System.out.println(user.getName()+" VS "+computer.getName()); System.out.println("對戰次數:"+count+"\n\n"); System.out.println("姓名\t得分"); System.out.println(user.getName()+"\t"+user.getScore()); System.out.println(computer.getName()+"\t"+computer.getScore()+"\n"); if(user.getScore()>computer.getScore()){ System.out.println("結果:恭喜恭喜"); }else if(user.getScore()<computer.getScore()){ System.out.println("結果:再接再厲"); }else{ System.out.println("結果:平局"); } System.out.println("--------------------------------"); } }
測試類
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Game game = new Game(); game.init(); } }
這樣猜拳小遊戲就實現瞭。
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。