java定义方法猜石头剪刀布
时间: 2023-12-18 21:29:30 浏览: 207
以下是Java定义方法猜石头剪刀布的示例代码:
```java
import java.util.Scanner;
public class GuessGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入玩家姓名:");
String playerName = input.nextLine();
System.out.println("欢迎来到猜拳游戏," + playerName + "!");
int total = 0; // 总局数
int win = 0; // 胜利局数
int lose = 0; // 失败局数
int draw = 0; // 平局数
while (true) {
System.out.println("请选择出拳(1-石头,2-剪刀,3-布,0-退出游戏):");
int playerFist = input.nextInt();
if (playerFist == 0) {
break;
}
int computerFist = (int) (Math.random() * 3) + 1;
System.out.println("电脑出拳:" + computerFist);
if (playerFist == computerFist) {
System.out.println("平局!");
draw++;
} else if (playerFist == 1 && computerFist == 2 || playerFist == 2 && computerFist == 3 || playerFist == 3 && computerFist == 1) {
System.out.println("恭喜你,你赢了!");
win++;
} else {
System.out.println("很遗憾,你输了!");
lose++;
}
total++; }
System.out.println("游戏结束," + playerName + "的战绩如下:");
System.out.println("总局数:" + total);
System.out.println("胜利局数:" + win);
System.out.println("失败局数:" + lose);
System.out.println("平局数:" + draw);
System.out.println("胜率:" + (double) win / total * 100 + "%");
}
}
```
阅读全文