-1

我有一个运行良好的 java 程序,但不知何故,加班它失败了。我真的不知道怎么做,也不知道为什么,所以我希望一双新的眼睛能帮助解决这个问题。请帮忙,谢谢!该程序是一个简单的“扑克”代码,从一副牌中随机输出 5 张牌。

该程序在我第一次运行时正在运行,但由于某种奇怪的原因它不再运行。

I keep getting the errors...
"cannot resolve symbol rank.length",
"cannot resolve symbol suit.length",
"Package java.Random does not exist"

该程序:

import java.Random.*;
import java.io.*;
import java.util.*;

public class Poker {

    public static void main(String[] args) {

        //initialize variables
        int suits = suit.length;
        int ranks = rank.length;
        int n = suits * ranks;
        //counter
        int m = 5;

         // create a deck of 52 cards
         String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
         String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
                           "Jack", "Queen", "King", "Ace"
                         };


         // initialize deck
         String[] deck = new String[n];
         for (int i = 0; i < ranks; i++) { 
             for (int j = 0; j < suits; j++) { 
                 deck[suits*i + j] = rank[i] + " of " + suit[j]; 

         }
         }

        // create random 5 cards
        for (int i = 0; i < m; i++)  {
            int r = i + (int) (Math.random() * (n-i));
            String t = deck[r];
            deck[r] = deck[i];
            deck[i] = t;
        }

        // print results
        for (int i = 0; i < m; i++){
            System.out.println(deck[i]);

        }        
    }
}
4

3 回答 3

3

无法解析符号rank.length

rank.length那是因为你在声明之前尝试访问rank

尝试:

//first declare arrays
// create a deck of 52 cards
String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
                   "Jack", "Queen", "King", "Ace"
                 };

//then find out their length
int suits = suit.length;
int ranks = rank.length;
int n = suits * ranks;
//counter
int m = 5;

包 java.Random 不存在

这是真的,它是import java.util.Random,不是import java.Random.*java.Random包不存在。

于 2013-10-01T22:27:12.900 回答
0
  int suits = suit.length;
  int ranks = rank.length;
  int n = suits * ranks;

the above lines should be placed below

String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };
String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
                           "Jack", "Queen", "King", "Ace"
                };

also remove import java.Random.* 
于 2013-10-01T22:30:31.597 回答
0

首先,这个包java.Random真的不存在。但是我看不出你在哪里使用Random,所以只需删除那个 import 语句。即使您正在使用Random,您也已经使用java.util.*.

对于其他两个错误,您在使用它们后声明了您所引用的变量 (suit和)。rank在使用它们之前声明它们。

于 2013-10-01T22:29:25.030 回答