import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 创建 Scanner 对象,用于获取用户输入
Scanner scanner = new Scanner(System.in);
// 创建 Random 对象,用于生成随机数
Random random = new Random();
// 循环进行猜拳
while (true) {
// 提示用户输入出拳决策
System.out.println("请出拳:1. 剪刀 2. 石头 3. 布");
int player = scanner.nextInt();
// 生成随机数,模拟电脑出拳
int computer = random.nextInt(3) + 1;
// 判断胜负
if (player == computer) {
// 平局
System.out.println("平局");
} else if ((player == 1 && computer == 2) || (player == 2 && computer == 3) || (player == 3 && computer == 1)) {
// 电脑胜利
System.out.println("电脑胜利");
} else {
// 玩家胜利
System.out.println("玩家胜利");
}
}
}
}
import java.util.Scanner;
public class AiFingerGuessingGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userChoice;
String computerChoice;
int computerRandom;
while (true) {
// Prompt user to enter their choice
System.out.println("Enter your choice (rock, paper, or scissors): ");
userChoice = input.nextLine();
// Generate random number for computer's choice
computerRandom = (int)(Math.random() * 3);
// Convert random number to corresponding choice
if (computerRandom == 0) {
computerChoice = "rock";
} else if (computerRandom == 1) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
// Compare choices and determine winner
if (userChoice.equals(computerChoice)) {
System.out.println("It's a tie!");
} else if (userChoice.equals("rock")) {
if (computerChoice.equals("scissors")) {
System.out.println("You win!");
} else {
System.out.println("Computer wins.");
}
} else if (userChoice.equals("paper")) {
if (computerChoice.equals("rock")) {
System.out.println("You win!");
} else {
System.out.println("Computer wins.");
}
} else if (userChoice.equals("scissors")) {
if (computerChoice.equals("paper")) {
System.out.println("You win!");
} else {
System.out.println("Computer wins.");
}
}
// Prompt user to play again
System.out.println("Do you want to play again? (yes/no)");
String playAgain = input.nextLine();
if (playAgain.equals("no")) {
break;
}
}
}
}
东软?