- /**
- * 猜拳游戏
- * 1.通过输入数字,来出拳,也可随机出拳
- * 2.没有提供程序退出命令
- * */
- import java.util.Random;
- import java.util.Scanner;
- public class fingerGuessinGame {
-
- private static int number = punch.values().length ; //获得枚举punch的length();
- private Scanner input = new Scanner(System.in);
-
- public static void main(String[] args) {
- fingerGuessinGame f = new fingerGuessinGame();
- System.out.println("1:石头,2:剪刀,3:布,其他数字均为石头,0为随机");
- while (true)
- f.punch();
- }
-
- private void punch()
- {
- System.out.print("请输入: ");
-
- int i = input.nextInt(); //获得输入的数字
- punch p = getRandom(i); //从输入的数字得到出拳
- System.out.print(p.getName()+" k ");
-
- punch random = getRandom(0); //电脑出拳
- System.out.print(random.getName());
-
-
- if( p.win.equals(random.toString()))
- System.out.println(" 这局你赢");
- else if(p.lose.equals(random.toString()))
- System.out.println(" 这局你输");
- else
- System.out.println(" 平局");
- }
-
- public static punch getRandom(int i){
- if(i==0) //如果i为0,就随机出拳
- i = new Random().nextInt(number);
- switch ((int) i){
- case 1:
- return punch.stone;
- case 2:
- return punch.scissors;
- case 3:
- return punch.cloth;
- default: return punch.stone;
- }
- }
-
- }
- enum punch
- {
- stone("石头","scissors","cloth"),
- scissors("剪刀","cloth","stone"),
- cloth("布","stone","scissors");
- private String name;
- String win; //出拳赢
- String lose; //出拳输
-
- public String getName()
- {
- return name;
- }
-
-
- private punch(String name,String win,String lose)
- {
- this.name = name;
- this.win = win;
- this.lose = lose;
- }
- }
复制代码 |