Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("欢迎来到石头、剪刀、布游戏!");
System.out.println("请输入你的选择(1代表石头,2代表剪刀,3代表布):");
int userChoice = scanner.nextInt();
int computerChoice = random.nextInt(3) + 1;
String userInput = "";
String computerInput = "";
switch (userChoice) {
case 1:
userInput = "石头";
break;
case 2:
userInput = "剪刀";
case 3:
userInput = "布";
default:
System.out.println("无效的选择,请重新输入!");
return;
}
switch (computerChoice) {
computerInput = "石头";
computerInput = "剪刀";
computerInput = "布";
System.out.println("你选择了:" + userInput);
System.out.println("电脑选择了:" + computerInput);
if (userChoice == computerChoice) {
System.out.println("平局!");
} else if ((userChoice == 1 && computerChoice == 2) ||
(userChoice == 2 && computerChoice == 3) ||
(userChoice == 3 && computerChoice == 1)) {
System.out.println("你赢了!");
} else {
System.out.println("你输了!");
scanner.close();
Sign in to make a reply
韩承欣
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.println("欢迎来到石头、剪刀、布游戏!");
System.out.println("请输入你的选择(1代表石头,2代表剪刀,3代表布):");
int userChoice = scanner.nextInt();
int computerChoice = random.nextInt(3) + 1;
String userInput = "";
String computerInput = "";
switch (userChoice) {
case 1:
userInput = "石头";
break;
case 2:
userInput = "剪刀";
break;
case 3:
userInput = "布";
break;
default:
System.out.println("无效的选择,请重新输入!");
return;
}
switch (computerChoice) {
case 1:
computerInput = "石头";
break;
case 2:
computerInput = "剪刀";
break;
case 3:
computerInput = "布";
break;
}
System.out.println("你选择了:" + userInput);
System.out.println("电脑选择了:" + computerInput);
if (userChoice == computerChoice) {
System.out.println("平局!");
} else if ((userChoice == 1 && computerChoice == 2) ||
(userChoice == 2 && computerChoice == 3) ||
(userChoice == 3 && computerChoice == 1)) {
System.out.println("你赢了!");
} else {
System.out.println("你输了!");
}
scanner.close();