XIA.人机猜拳

 1 package test1_game;
 2 /**
 3  * 电脑玩家类
 4  * 
 5  * @author ljj
 6  * 
 7  */
 8 import java.util.Scanner;
 9 public class Computer {
10     //定义电脑类的成员方法
11     public String cName;//电脑用户的昵称
12     public int cScore;//电脑用户的积分
13     public String cType;//电脑出拳类型
14     
15     public int showFist(){
16         //电脑出拳类型
17         int choice =0;
18         //
19         choice = (int)(Math.random()*3+1);
20         switch(choice){
21         case 1:
22             cType="剪刀";
23             break;
24         case 2:
25             cType="石头";
26             break;
27         case 3:
28             cType="布";
29             break;
30         }
31         System.out.println(cName+"出拳:"+cType);
32         return choice;
33     }
34 }
 1 package test1_game;
 2 /**
 3  * 用户玩家类
 4  * @author ljj
 5  *
 6  */
 7 import java.util.Scanner;
 8 public class Player {
 9     //玩家成员变量的定义
10     public String pName;//玩家昵称
11     public int pScore;//玩家积分
12     public String pType;//玩家出拳类型
13     
14     /**
15      * 玩家出拳的方法
16      * @return choice
17      */
18     public int showFist(){
19         //用户选择输入要出的拳类型,返回选择的数
20         //创建用户录入对象
21         Scanner input = new Scanner(System.in);
22         int choice=0;
23         
24         System.out.println("请出拳:1。剪刀  2.石头  3.布");
25         choice = input.nextInt();
26         
27         //while判断录入的数字是否正确
28         while(choice <0||choice>3){
29             System.out.println("您选择的有误,请重新选择:");
30             choice = input.nextInt();
31         }
32         
33         switch(choice){
34         case 1:
35             pType="剪刀";
36             break;
37         case 2:
38             pType="石头";
39             break;
40         case 3:
41             pType="布";
42             break;
43         }
44         //输出选择的出拳类型
45         System.out.println("您选择的是:"+pType);
46         return choice;
47         
48     }
49     
50 
51 }
 1 package test1_game;
 2 /**
 3  * 电脑玩家类
 4  * 
 5  * @author ljj
 6  * 
 7  */
 8 import java.util.Scanner;
 9 public class Computer {
10     //定义电脑类的成员方法
11     public String cName;//电脑用户的昵称
12     public int cScore;//电脑用户的积分
13     public String cType;//电脑出拳类型
14     
15     public int showFist(){
16         //电脑出拳类型
17         int choice =0;
18         //
19         choice = (int)(Math.random()*3+1);
20         switch(choice){
21         case 1:
22             cType="剪刀";
23             break;
24         case 2:
25             cType="石头";
26             break;
27         case 3:
28             cType="布";
29             break;
30         }
31         System.out.println(cName+"出拳:"+cType);
32         return choice;
33     }
34 }
 1 package test1_game;
 2 /**
 3  * 测试类
 4  * @author ljj
 5  *
 6  */
 7 public class Test {
 8 
 9     /**
10      * @param args
11      */
12     public static void main(String[] args) {
13         // TODO Auto-generated method stub
14         //创建游戏类的对象
15         Game game = new Game();
16         
17         //调用游戏类的开始游戏的方法
18         game.startGame();
19 
20     }
21 
22 }
年轻人能为世界年轻人能为世界做些什么
原文地址:https://www.cnblogs.com/twinkle-star/p/9326542.html