import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
//-- 猜拳
for (int i = 0; i <120 ;i++ ) {
System.out.print("*");
if (i == 59) {
System.out.println("\n\n\t\t\t猜拳,开始\n");
}
}
//-- 打印出拳规则
System.out.println("\n出拳规则: 1.剪刀 2.石头 3.布");
System.out.print("请选择对方角色: (1.刘备 2.孙权 3.曹操):");
//-- 提供Scanner;
Scanner scan = new Scanner(System.in);
int choose = scan.nextInt();
String cpuName = "";
switch(choose){
case 1:
cpuName = "刘备";
break;
case 2:
cpuName = "孙权";
break;
case 3:
cpuName = "曹操";
break;
default:
break;
}
System.out.print("请输入你的姓名:");
String playName = scan.next();
System.out.println(playName + " V S " + cpuName);
System.out.print("\n要开始吗?(y/n):");
String temp = scan.next();
if ("n".equalsIgnoreCase(temp)) {
System.out.println("游戏结束");
return ;
}
for(;;){
System.out.println("***************************************");
System.out.print("请出拳: 1.剪刀 2.石头 3.布(输入相应数字)");
//-- 获取用户的选择,并打印
int playChoose = scan.nextInt();
switch(playChoose){
case 1:
System.out.println("你出拳: 剪刀");
break;
case 2:
System.out.println("你出拳: 石头");
break;
case 3:
System.out.println("你出拳: 布");
break;
default:
break;
}
//-- 随机数生成电脑的选择
//-- Math.random() [0,1) 123
//-- [0,1) * 3 -> [0,3)
//-- [0,1) * 3 -> [0,3) +
//-- [1,4) -> 1 2 3
int cpuChoose = (int)(Math.random()*3 + 1);
switch(cpuChoose){
case 1:
System.out.println(cpuName + "出拳:剪刀");
break;
case 2:
System.out.println(cpuName + "出拳:石头");
break;
case 3:
System.out.println(cpuName + "出拳:布");
break;
}
//-- 对输赢做判断
if (cpuChoose == playChoose) {
System.out.println("平局");
}else if(playChoose == 1 && cpuChoose == 3||
playChoose == 2 && cpuChoose == 1||
playChoose == 3 && cpuChoose == 2){
//-- 石头2 剪刀1
//-- 剪刀1 布3
//-- 布3 石头2
System.out.println(playName + "K O");
}else{
System.out.println(cpuName + "K O");
}
System.out.println("是否开始下一局:(y/n):");
temp = scan.next();
if (!("y".equalsIgnoreCase(temp))) {
break;
}
}
System.out.println("欢迎再来玩!");
}
}