java石头剪刀布源代码
时间: 2023-11-03 12:05:23 浏览: 121
石头剪刀布(源码)
以下是Java实现石头剪刀布游戏的源代码:
```
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] choices = {"rock", "paper", "scissors"};
int computerChoice = (int) (Math.random() * 3);
System.out.print("Enter your choice (rock, paper, or scissors): ");
String userChoice = input.nextLine().toLowerCase();
System.out.println("Computer's choice: " + choices[computerChoice]);
if (userChoice.equals(choices[computerChoice])) {
System.out.println("It's a tie!");
} else if (userChoice.equals("rock") && choices[computerChoice].equals("scissors") ||
userChoice.equals("paper") && choices[computerChoice].equals("rock") ||
userChoice.equals("scissors") && choices[computerChoice].equals("paper")) {
System.out.println("You win!");
} else {
System.out.println("Computer wins!");
}
}
}
```
阅读全文