我有一个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]);
}
}
}
答案 0 :(得分: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
包不存在。
答案 1 :(得分:0)
首先,包java.Random
确实不存在。但是我无论如何都看不到你在哪里使用Random
,所以只需删除那个import语句。即使您使用Random
,也可以使用java.util.*
导入它。
对于其他两个错误,您在使用它们后声明了您引用的变量(suit
和rank
)。在使用它们之前声明它们。
答案 2 :(得分: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.*