playPenaltyShootOut: 模拟点球大战 。每个队伍的两名球员,每个人有五次点球机会 (进没 //进,有 0,1 随机数决定,0 代表不进,1 代表进球,随机数 0 出现的概率为 20%,1 出现的 //概率为 80%。最后将这两名球员的进球数加在一起,进球数多的那一队胜利。 若点球大战 //进球数依然相同,则随机决定胜。用java写出
时间: 2024-02-22 12:57:29 浏览: 19
好的,以下是用Java实现模拟点球大战的代码:
```java
import java.util.Random;
public class PenaltyShootOut {
public static void main(String[] args) {
// 定义两个球队的进球数
int team1Score = 0;
int team2Score = 0;
// 模拟两个球队的两名球员依次进行五次点球
for (int i = 0; i < 5; i++) {
// 球员1的点球
int player1Score = shoot();
// 球员2的点球
int player2Score = shoot();
// 统计两名球员的进球数
team1Score += player1Score;
team2Score += player2Score;
}
// 判断哪个球队胜利
if (team1Score > team2Score) {
System.out.println("Team 1 wins!");
} else if (team2Score > team1Score) {
System.out.println("Team 2 wins!");
} else {
// 如果两个球队进球数相同,则随机决定胜负
Random random = new Random();
int winner = random.nextInt(2) + 1;
System.out.println("Team " + winner + " wins!");
}
}
// 模拟一次点球,返回进球数(0或1)
public static int shoot() {
Random random = new Random();
int score = random.nextInt(10) < 2 ? 0 : 1; // 0的概率为20%,1的概率为80%
return score;
}
}
```
这段代码会模拟两个球队的两名球员进行五次点球,然后统计两个球队的进球数,最后判断哪个球队胜利。如果两个球队进球数相同,则随机决定胜负。