如果声明布局不对

时间:2012-10-24 15:59:57

标签: java if-statement

我正在尝试创建一个程序来为卡分配一个值,然后根据哪个玩家获得最高卡然后他们赢得一个点现在我无法工作的区域是最后一个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");
            }     
      }

1 个答案:

答案 0 :(得分:2)

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