0

我正在尝试自己学习 Java,但我没有程序员的背景。我正在尝试基于 Sedgewick 和 Wayne 的“Java 编程”创建一个程序,该程序创建一副纸牌数组并打印出 5 张随机纸牌的“f”手。到目前为止,我已经配置了我的程序,我收到一条错误消息,上面写着“i”已在 main.xml 中定义。我究竟做错了什么?

public class Deal
{
    public static void main(String[] args)
    {

        String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };

        String[] rank = 
        {
            "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "Jack", "Queen", "King", "Ace"
        };
        // Creates the deck
        String[] deck = new String[suit.length * rank.length];
        for (int i = 0; i < suit.length; i++)
            for (int j = 0; j < rank.length; j++)
                deck[rank.length*i + j] = rank[i] + " of " + suit[j];

        int f = Integer.parseInt(args[0]);

        for (int i = 0; i < f; i++) 
        {
           int M = 5;    
           int N = 52;   

            // create permutation 0, 1, ..., N-1
           int[] perm = new int[N];
           for (int i = 0; i < N; i++)
               perm[i] = i;

           // create random sample in perm[0], perm[1], ..., perm[M-1]
           for (int i = 0; i < M; i++)  {

           // random integer between i and N-1
           int r = i + (int) (Math.random() * (N-i));

           // swap elements at indices i and r
           int t = perm[r];
           perm[r] = perm[i];
           perm[i] = t;
       }

           // print results
           for (int i = 0; i < M; i++)
               System.out.println(deck[perm[i]] + " ");
           System.out.println();
        }
    }
}
4

1 回答 1

6

该错误意味着......您正在尝试定义i已定义的变量。没有什么比这更重要的了。

你有

for (int i = 0; ...) {
  ...
  for (int i = 0; ...) { // error here.
  ...
  }
}

例如,在内部循环中,您必须使用不同的变量名j

于 2013-07-04T20:03:06.403 回答