在老师的指导下做了一个猜拳的小游戏,有一个computer类,person类,gamerule类,test测试类,以下是我的具体代码,希望大家指正,谢谢。
1 package com.game.shihan; 2 3 public class Computer { 4 String name; 5 int score; 6 7 public int showFist(){ 8 9 int num = (int)(Math.random() * 10) % 3 + 1; 10 switch(num){ 11 case 1: 12 System.out.println(name + "出剪刀"); 13 break; 14 case 2: 15 System.out.println(name + "出石头"); 16 break; 17 case 3: 18 System.out.println(name + "出布"); 19 break; 20 } 21 return num; 22 23 } 24 25 }
1 package com.game.shihan; 2 import java.util.Scanner; 3 public class Person { 4 String name; 5 int score; 6 Scanner input = new Scanner(System.in); 7 public int showFist(){ 8 9 System.out.println("请出拳:1.剪刀 2.石头 3.布"); 10 int num = input.nextInt(); 11 switch(num){ 12 case 1: 13 System.out.println("你出剪刀"); 14 break; 15 case 2: 16 System.out.println("你出石头"); 17 break; 18 case 3: 19 System.out.println("你出布"); 20 break; 21 } 22 return num; 23 } 24 25 }
1 package com.game.shihan; 2 import java.util.Scanner; 3 public class GameRule { 4 Scanner input = new Scanner(System.in); 5 int personNum = 0; 6 int computerNum = 0; 7 int count = 0; 8 Person person = new Person(); 9 Computer computer = new Computer(); 10 //开始游戏 11 public void StartGame(){ 12 13 person.score = 0; 14 computer.score = 0; 15 System.out.println("欢迎进入人机猜拳游戏"); 16 System.out.println("请输入你的名字:"); 17 person.name = input.next(); 18 System.out.println("你想与谁对决:1.C罗 2.贝尔 3.本泽马"); 19 int role = input.nextInt(); 20 21 switch(role){ 22 case 1: 23 computer.name = "C罗"; 24 break; 25 case 2: 26 computer.name = "贝尔"; 27 break; 28 case 3: 29 computer.name = "本泽马"; 30 break; 31 } 32 System.out.println("你确定开始游戏吗?(y/n)"); 33 if(input.next().equals("y")){ 34 System.out.println("游戏开始:" + person.name +"\t" + "VS" + "\t" + computer.name); 35 do{ 36 personNum = person.showFist(); 37 computerNum = computer.showFist(); 38 if(isWin() == 0){ 39 System.out.println("胜负未分"); 40 }else if(isWin() == 1){ 41 person.score++; 42 System.out.println("你赢了!"); 43 }else{ 44 computer.score++; 45 System.out.println("电脑赢了!"); 46 } 47 count++; 48 System.out.println("继续PK吗?(y/n)"); 49 }while(input.next().equals("y")); 50 } 51 52 } 53 //开始游戏 54 //判断胜负平 55 public int isWin(){ 56 if(personNum == computerNum){ 57 return 0;//平局 58 }else if(personNum == 1 && computerNum == 3 || personNum == 2 && computerNum == 1 || personNum == 3 && computerNum == 2){ 59 return 1;//玩家胜出 60 }else{ 61 return -1;//电脑胜出 62 } 63 } 64 //判断胜负平 65 //统计战果 66 public void showResult(){ 67 68 System.out.println("共比试了" + count + "场,你赢了" + person.score + "场," + computer.name + "赢了" + computer.score + "场,平局" + (count - person.score-computer.score) +"场"); 69 if(person.score > computer.score){ 70 System.out.println("恭喜,你赢了!"); 71 }else if(person.score == computer.score){ 72 System.out.println("平局"); 73 }else{ 74 System.out.println("哈哈," + computer.name + "赢了"); 75 } 76 } 77 //统计战果 78 }
1 package com.game.shihan; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 GameRule g = new GameRule(); 7 g.StartGame(); 8 g.showResult(); 9 } 10 11 }
浙公网安备 33010602011771号