石头剪刀布java十局
时间: 2023-12-22 22:29:10 浏览: 66
以下是一个Java实现的石头剪刀布游戏的示例代码,可以进行十局游戏:
```java
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
String[] choices = {"石头", "剪刀", "布"};
int playerScore = 0;
int computerScore = 0;
for (int i = 0; i < 10; i++) {
System.out.println("第 " + (i + 1) + " 局:");
System.out.println("请输入你的选择(0-石头,1-剪刀,2-布):");
Scanner scanner = new Scanner(System.in);
int playerChoice = scanner.nextInt();
if (playerChoice < 0 || playerChoice > 2) {
System.out.println("输入无效,请重新输入!");
i--;
continue;
}
int computerChoice = new Random().nextInt(3);
System.out.println("电脑选择:" + choices[computerChoice]);
if (playerChoice == computerChoice) {
System.out.println("平局!");
} else if ((playerChoice == 0 && computerChoice == 1) ||
(playerChoice == 1 && computerChoice == 2) ||
(playerChoice == 2 && computerChoice == 0)) {
System.out.println("你赢了!");
playerScore++;
} else {
System.out.println("电脑赢了!");
computerScore++;
}
System.out.println("当前得分:你-" + playerScore + ",电脑-" + computerScore);
System.out.println();
}
if (playerScore > computerScore) {
System.out.println("你赢得了比赛!");
} else if (playerScore < computerScore) {
System.out.println("电脑赢得了比赛!");
} else {
System.out.println("比赛结束,平局!");
}
}
}
```
阅读全文