用java写一个剪刀石头布的程序
时间: 2023-10-28 12:06:12 浏览: 80
java开发的石头、剪刀、布游戏源码
以下是用Java编写的剪刀石头布游戏程序:
```java
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your choice (1: Rock, 2: Paper, 3: Scissors): ");
int userChoice = scanner.nextInt();
// Generate a random number between 1 and 3
int computerChoice = (int)(Math.random() * 3) + 1;
// Determine the winner
if (userChoice == computerChoice) {
System.out.println("It's a tie!");
} else if (userChoice == 1 && computerChoice == 3 ||
userChoice == 2 && computerChoice == 1 ||
userChoice == 3 && computerChoice == 2) {
System.out.println("You win!");
} else {
System.out.println("Computer wins!");
}
}
}
```
此程序要求用户输入选择(1表示石头,2表示纸,3表示剪刀),然后生成一个1到3之间的随机数作为计算机的选择,最后确定胜者并输出结果。
阅读全文