猜丁壳小游戏:
import java.util.Random;
import java.util.Scanner;
//石头2剪刀1 布0
public class test2Stone {
public static void main(String[] args) {
Random num = new Random();
int num11 = num.nextInt(3);//随机抽取0到3之间的数字除了3
Scanner input = new Scanner(System.in);//键盘输入
while (true) {
System.out.println("请输入0~3之间的数");
int guess = input.nextInt();
if (guess == num11) {//判断随机数和键盘输入数是否相同
System.out.println("平局!,你出了" + guess + "电脑出了" + num11 + "请重新来");
} else if (guess < num11 && num11 - 1 == guess) {//判断随机数和键盘输入数是否不同
System.out.println("电脑赢了!,你出了" + guess + "电脑出了" + num11 + "请重新来");
} else if (0 > guess || guess >= 3) {//判断随机数的取值范围
System.out.println("数据错误,请重新输入");
} else {
System.out.println("你赢了!,你出了" + guess + "电脑出了" + num11);
break;//终止循环
}
}
System.out.println("游戏结束");
}
} |
|