国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

人機猜拳之Java面向對象小游戲

2019-11-06 09:38:24
字體:
來源:轉載
供稿:網友

人機猜拳小游戲,只要用到的java面向對象的思維模式。本游戲中有游戲玩家和計算機兩個參與者。玩家和計算機都有的屬性是姓名和得分。共分為4個類。下圖是游俠小程序控制臺截圖:開始比賽 第一局和第二局比賽 三局比賽及結果 以下是源代碼: Computer.java類

package com.whf.guess.game;/** * @author :辰 * @version * 創建時間:2017年2月24日 上午8:38:29 * */public class Computer { String name = "電腦";//名字 int score = 0;//積分 public int showFist() { //產生隨機數 int show = (int) ((Math.random()*10)%3+1); // 產生隨機數,表示電腦出拳 //輸出出拳結果并返回 switch (show) { case 1: System.out.Person.java 類

package com.whf.guess.game;import java.util.Scanner;/** * @author :辰 * @version * 創建時間:2017年2月24日 上午8:45:41 * */public class Person { String name = "匿名"; //名字 int score = 0; //積分 public int showFist(){ //接收用戶的選擇 Scanner input = new Scanner(System.in); System.out.println("/n請出拳:1.剪刀 2.石頭 3.布"); int show = input.nextInt(); //輸出出拳結果,并且返回 switch (show) { case 1: System.out.println(name+"你出拳:剪刀"); break; case 2: System.out.println(name+"你出拳:石頭"); break; case 3: System.out.println(name+"你出拳:布"); break; } return show; }}

Game.java類。包含游戲的比賽核心代碼

package com.whf.guess.game;import java.util.Scanner;/** * @author :辰 * @version * 創建時間:2017年2月24日 上午8:37:06 * */public class Game<computer> { Person person ; //甲方 Computer computer;// 乙方 int count;// 對戰次數 public void inital(){ person = new Person(); computer = new Computer(); count = 0; } //開始游戲 public void startGame(){ System.err.println("-------------歡迎進入猜拳游戲---------------"); System.out.println(); System.out.println("******************************************"); System.out.println("——————出拳規則:1.剪刀 2.石頭 3.布——————————"); System.out.println("-------------》》》猜拳開始《《《------------"); System.err.println("-------------------------------------------"); Scanner input = new Scanner(System.in); String exit="n";//退出系統 do { inital();//初始化 //選擇對手 System.out.println("選擇你的對手1.馬云 2.馬化騰 3.馬大姐 "); int role = input.nextInt(); if (role ==1) { computer.name="馬云"; }else if (role == 2) { computer.name="馬化騰"; }else if (role == 3) { computer.name="馬大姐"; } //游戲玩家輸入姓名 System.out.println("請輸入你的姓名:"); person.name=input.next(); System.out.println(person.name+" VS "+computer.name+"對戰/n"); //開始游戲 System.out.println("要開始嗎?(y/n)"); //開始一局游戲 String start = input.next(); int perFist;//游戲用戶出拳 int comFist;//計算機出拳 while (start.equals("y")) { //出拳 perFist = person.showFist(); comFist = computer.showFist(); //判斷勝負 if ((perFist==1&&comFist==1)||(perFist==2&&comFist==2)||(perFist==3&&comFist==3)) { System.err.println("結果:和局,真衰!/n");//平局 }else if ((perFist==2&&comFist==1)||(perFist==3&&comFist==2)||(perFist==1&&comFist==3)) { System.err.println("結果:你贏了,真帥!/n");//勝利 person.score++; }else { System.err.println("結果是:^_^,你輸了,下載再戰!/n");//計算機贏 computer.score++; } count++; System.out.println("是否進行下一輪(y/n)"); start = input.next(); } //顯示結果 showResult(); System.out.println("/n要開始下一局嗎?(y/n):"); exit = input.next(); System.out.println(); } while (!exit.equals("n")); System.out.println("系統退出"); } private void showResult() { // TODO Auto-generated method stub //顯示對戰次數 System.out.println("------------------------------"); System.out.println(computer.name+" VS "+person.name); System.out.println("對戰次數:"+count); //顯示最終得分 System.out.println("/n姓名/t得分"); System.out.println(person.name+"/t"+person.score); System.out.println(computer.name+"/t"+computer.score+"/n"); //顯示對戰結果 int result = calcResult(); if (result==1) { System.err.println("結果:打成平手,下次再和你一分高下"); }else if (result==2) { System.err.println("結果:恭喜恭喜你贏得比賽"); }else { System.err.println("結果:不氣餒,下次再來");//計算機贏 } System.out.println("--------------------------"); } //計算比賽結果 private int calcResult() { // TODO Auto-generated method stub if (person.score == computer.score) { return 1;//戰平 }else if (person.score > computer.score) { return 2;//游戲玩家贏 }else { return 3;//惜敗 } }}

StartGuess.java類

package com.whf.guess.game;/** * @author :辰 * @version * 創建時間:2017年2月24日 上午9:35:36 * */public class StartGuess { public static void main(String[] args) { Game game = new Game(); game.inital(); game.startGame(); }}

以上就是完整的人機猜拳java面向對象的小游戲源碼.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兴化市| 澄城县| 宁夏| 娄底市| 沙河市| 祁连县| 繁峙县| 休宁县| 西乌| 梅州市| 乐陵市| 泽州县| 新宁县| 许昌县| 新宁县| 庆城县| 扬中市| 南城县| 彭山县| 佛坪县| 桐庐县| 涟水县| 定安县| 正安县| 石屏县| 密山市| 航空| 马公市| 福建省| 锡林郭勒盟| 奈曼旗| 育儿| 县级市| 龙南县| 藁城市| 长葛市| 建湖县| 东乌珠穆沁旗| 普洱| 西吉县| 武邑县|