package com.xiaoyouxi;
import java.util.Random;
import java.util.Scanner;
//和电脑比猜拳, 先获得三胜的赢
public class CaiQuan {
public static void main(String[] args) {
Random r = new Random();
Scanner sc = new Scanner(System.in);
int z = 0;
int x = 0;
int y = 0;
while (true) {
System.out.println("请玩家输入一个1~3的数字");
int b = sc.nextInt();
if (b == 1) {
System.out.println("玩家出的是石头");
} else if (b == 2) {
System.out.println("玩家出的是剪刀");
} else if (b == 3) {
System.out.println("玩家出的是布");
} else {
z++;
if (z == 1) {
System.out.println("输入有误,请重新输入");
}
if (z == 2) {
System.out.println("逗逼?");
}
if (z == 3) {
System.out.println("sb");
break;
}
continue;
}
int a = r.nextInt(3) + 1;
if (a == 1) {
System.out.println("电脑出的是石头");
} else if (a == 2) {
System.out.println("电脑出的是剪刀");
} else if (a == 3) {
System.out.println("电脑出的是布");
} else {
System.out.println("输入有误,请重新输入");
}
if ((a == 1 & b == 3) || (a == 2 & b == 1) || (a == 3 & b == 2)) {
System.out.println("玩家赢了");
x++;
System.out.println("玩家已经赢了"+x+"局");
} else if ((a == 1 & b == 2) || (a == 2 & b == 3)
|| (a == 3 & b == 1)) {
System.out.println("电脑赢了");
y++;
System.out.println("电脑已经赢了"+y+"局");
} else if (a == b) {
System.out.println("平局");
}
System.out.println("-----------");
if(x==3){
System.out.println("玩家先获得三胜,玩家赢得这场比赛");
//break; 只能用在switch和循环体中
System.exit(0);
}
if(y==3){
System.out.println("电脑先获得三胜,电脑赢得这场比赛");
System.exit(0);
}
}
}
} |
|