public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
//计数器
int count=0,countHuman=0,countRobot=0;
//用户连续玩三局
while (count<3){
System.out.println("0:布");
System.out.println("1:剪刀");
System.out.println("2:石头");
System.out.println("请选择要出的选项:");
int human = sc.nextInt();
int robot = random.nextInt(3);
switch (human) {
case 0: System.out.println("用户出的是:布");break;
case 1: System.out.println("用户出的是:剪刀");break;
case 2: System.out.println("用户出的是:石头");break;
}
switch (robot) {
case 0: System.out.println("机器人出的是:布");break;
case 1: System.out.println("机器人出的是:剪刀");break;
case 2: System.out.println("机器人出的是:石头");break;
}
if(human == robot){
System.out.println("平局");
}else if(human==0 && robot==2){
System.out.println("用户赢");
countHuman++;
}else if(human==2 && robot==0){
System.out.println("机器赢");
countRobot++;
}else if (human > robot){
System.out.println("用户赢");
countHuman++;
}else {
System.out.println("机器赢");
countRobot++;
}
System.out.println("----------------------------");
count++;
}
System.out.println("用户赢"+countHuman+"次");
System.out.println("机器赢"+countRobot+"次");
if(countHuman==countRobot){
System.out.println("平局");
}else if (countHuman > countRobot){
System.out.println("最终用户赢");
}else {
System.out.println("最终机器赢");
}
}
优化
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
//计数器
int count = 0, countPilay = 0, countComputer = 0;
while (true) {
count++;
//选择出拳
System.out.println("请猜拳:0布,1剪刀,2石头");
int play = sc.nextInt();
int comptuer = random.nextInt(3);
//展示出拳
switch (play) {
case 0:
System.out.println("用户出的是:布");
break;
case 1:
System.out.println("用户出的是:剪刀");
break;
case 2:
System.out.println("用户出的是:石头");
break;
}
switch (comptuer) {
case 0:
System.out.println("机器人出的是:布");
break;
case 1:
System.out.println("机器人出的是:剪刀");
break;
case 2:
System.out.println("机器人出的是:石头");
break;
}
//比赢
if (play == comptuer) {
System.out.println("该局为平局");
} else if (play == 0 && comptuer == 2) {
System.out.println("该局用户赢");
countPilay++;
} else if (play == 2 && comptuer == 0) {
System.out.println("该局机器赢");
countComputer++;
} else if (play > comptuer) {
System.out.println("该局用户赢");
countPilay++;
} else {
System.out.println("该局机器赢");
countComputer++;
}
//三局两胜
if(count == 3){
if(countPilay==countComputer){
System.out.println("游戏结束,最终为平局");
}else if (countPilay>countComputer){
System.out.println("游戏结束,最终用户获胜");
}else {
System.out.println("游戏结束,最终电脑获胜");
}
//是否进行下一轮游戏
System.out.println("-------是否还进行游戏(Y/N)-------");
String inputs = sc.next();
if (inputs.equals("Y")) {
count=0;
countPilay = 0;
countComputer = 0;
continue;
} else if (inputs.equals("N")) {
break;
} else {
System.out.println("输入有误,退出程序!");
break;
}
}else if (countPilay == 2 ) {
System.out.println("游戏结束,最终用户获胜");
//是否进行下一轮游戏
System.out.println("-------是否还进行游戏(Y/N)-------");
String inputs = sc.next();
if (inputs.equals("Y")) {
count=0;
countPilay = 0;
countComputer = 0;
continue;
} else if (inputs.equals("N")) {
break;
} else {
System.out.println("输入有误,退出程序!");
break;
}
} else if (countComputer == 2) {
System.out.println("游戏结束,最终电脑获胜");
//是否进行下一轮游戏
System.out.println("-------是否还进行游戏(Y/N)-------");
String inputs = sc.next();
if (inputs.equals("Y")) {
count=0;
countPilay = 0;
countComputer = 0;
continue;
} else if (inputs.equals("N")) {
break;
} else {
System.out.println("输入有误,退出程序!");
break;
}
}
System.out.println("————————下一局————————");
}
}