3

我正在尝试创建一个程序来为卡片分配一个值,然后根据哪个玩家获得最高卡片然后他们赢得一分现在我无法工作的区域是最后一个 if 语句,因为它在每一轮而不是 7如所须。


import java.util.*;

public class Card {

    public static void main(String[] args) {

        int player1= 0;
        int player2 = 0;
        int i = 1;

        while ( i <= 7) {
            int player1Card = (int) (Math.random() * 13) + 1;
            int player2Card = (int) (Math.random() * 13) + 1;

            System.out.println("player 1 = " + player1Card);
            System.out.println("player 2 = " + player2Card);

           if (player1Card > player2Card) {
                System.out.println("Player 1 wins!!!");
                player1 = player1 + 1;
            } else if (player1Card == player2Card){
                System.out.println("It's a bore draw");
                player1 = player1 + 0;
                player2= player2 + 0;
            } else {
                System.out.println("Player 2 wins!!!!!");
                player2 = player2 + 1;
            }
            System.out.println("Player 1 points " + player1);
            System.out.println("Player 2 points " + player2);
            i++;

            if (player1 > player2) {
                System.out.println("The winner is player 1 with " + player1 + " points");
            } else if (player1 == player2) {
                System.out.println("Its a draw");
            } else {
                System.out.println("The winner is Player 2 with " + player2 + " points");
            }     
      }
4

1 回答 1

2

您的 if 语句需要在 while 循环结束之后出现。将最后一个移动}到要在所有 7 次运行后执行的 if 语句之前。

于 2012-10-24T16:03:50.833 回答