1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| import java.util.Scanner;
public class CaiQuan { final static int WIN = 1; final static int FAIL = 2; final static int TIE = 0;
int player, computer;
int count = 0;
int playerscore = 0; int computerscore = 0;
String cname, username;
String[] computers = { "刘备", "孙权", "曹操" };
String[] words = { "剪刀", "石头", "布" };
String[] info = { "和局,真衰!等着瞧吧!", "恭喜,你赢了!", "哈哈!,你输了!" };
void printResult() { System.out.println("-----------------------"); System.out.println("对战次数:" + count); System.out.println("姓名\t得分"); System.out.println(username + "\t" + playerscore); System.out.println(cname + "\t" + computerscore); if (playerscore > computerscore) System.out.println("结果: 恭喜恭喜,你的分数更高!"); else if (playerscore < computerscore) System.out.println("结果: 你的分数更低"); else System.out.println("结果:难分难解,平局!"); }
int getResult() { if ((player + 1) % 3 == computer) { computerscore++; return FAIL; } else if (player == computer) { return TIE; } else { playerscore++; return WIN; } }
public void initGame() { System.out.println(); System.out.println("******************欢迎进入游戏世界*********************"); System.out.println("\t\t***********"); System.out.println("\t\t**猜拳开始**"); System.out.println("\t\t***********"); System.out.println("出拳规则:1.剪刀2.石头3.布");
}
public void startGame() { initGame(); Scanner scanner = new Scanner(System.in); System.out.println("请选择对方角色:(1:刘备2.孙权3.曹操)"); cname = computers[scanner.nextInt() - 1]; System.out.println("请输入你的姓名:"); username = scanner.next(); System.out.println(username + " VS " + cname + " 对战"); System.out.println("要开始吗?(y/n)"); if ("n".equals(scanner.next())) { printResult(); System.exit(0); }
while (true) { count++; System.out.println("请出拳:1.剪刀2.石头3.布"); player = scanner.nextInt() - 1; computer = (int) (Math.random() * 3); System.out.println("你出拳:" + words[player]); System.out.println(cname + "出拳:" + words[computer]); System.out.println("结果:" + info[getResult()]); System.out.println("\n是否进入下一轮:(y/n)"); if ("n".equals(scanner.next())) { printResult(); System.exit(0); } } }
public static void main(String[] args) {
CaiQuan game = new CaiQuan(); game.startGame();
}
}
|