Discuss / Java / 这个随机数咋一直是1呢?不是1到3的随机数吗?

这个随机数咋一直是1呢?不是1到3的随机数吗?

Topic source

217119

#1 Created at ... [Delete] [Delete and Lock User]

public class Main {

public static void main(String[] args) {

System.out.println("please choice:");

System.out.println(" 1: Rock");

System.out.println(" 2: Scissors");

System.out.println(" 3: Paper");

// 用户输入:

int choice = 0;

Scanner scanner = new Scanner(System.in);

// 随机数

int random = 0;

// 计数

int count = 1;

while (count <= 3) {

// 计算机随机数 1, 2, 3:

random = (int)Math.random() * 3 + 1;

System.out.printf("第%d把游戏开始, ", count);

System.out.print("输入一个数字:");

choice = scanner.nextInt();

switch (choice) {

// TODO:

case 1 -> {

game(choice, random);

}

case 2 -> {

game(choice, random);

}

default -> {

game(choice, random);

}

};

count++;

}

System.out.println("游戏结束!");

}

public static void game(int choice, int random) {

System.out.println("它选的是:" + (random == 1 ? "石头" : (random == 2 ? "剪刀" : "布")));

if (choice == random) {

System.out.println("平局");

} else if (choice + 1 == random || choice - 2 == random) {

System.out.println("你赢了");

} else {

System.out.println("你输了");

}

}

}


  • 1

Reply