问题描述
因此,我试图用Java编写UNO,但仍在尝试生成卡片。 我不太确定这是什么问题,但是由于某种原因,我的代码在我的方法中捕获了一个错误。 我已经检查了我的代码几次,这不是语法错误,所以我当然不知道它是怎么回事。
我现在暂时停止对此进行编码,以便在大家告诉我出什么问题之前,我不会再产生任何错误,以便更轻松地对其进行修改。 请告诉我我做错了!
public class JavaUNO {
public static void main(String[] args) throws Exception {
boolean inProgress = false;
boolean drawCard = false;
String[][] playerDeck = {{}};
byte playerDeckLength = 0;
// MAIN OUTPUT
try {
// INITIALIZATION
Scanner scan = new Scanner(System.in);
// PROGRAM STARTING PROMPT
System.out.println("> Deck:");
// **PLAYER DECK INIT**
try {
System.out.println("> Cards Generated:");
while (playerDeckLength < 7) {
// **CARD GENERATION**
try {
// INITIALIZATION
double randType = Math.random();
double randColor = Math.random();
playerDeck[playerDeckLength][0] = "";
playerDeck[playerDeckLength][1] = "";
// GENERATES RANDOM CARD TYPE
if (randType < 0.066) {
playerDeck[playerDeckLength][0] = "0";
} else if (randType < 0.132) {
playerDeck[playerDeckLength][0] = "1";
} else if (randType < 0.198) {
playerDeck[playerDeckLength][0] = "2";
} else if (randType < 0.264) {
playerDeck[playerDeckLength][0] = "3";
} else if (randType < 0.33) {
playerDeck[playerDeckLength][0] = "4";
} else if (randType < 0.396) {
playerDeck[playerDeckLength][0] = "5";
} else if (randType < 0.462) {
playerDeck[playerDeckLength][0] = "6";
} else if (randType < 0.528) {
playerDeck[playerDeckLength][0] = "7";
} else if (randType < 0.594) {
playerDeck[playerDeckLength][0] = "8";
} else if (randType < 0.66) {
playerDeck[playerDeckLength][0] = "9";
} else if (randType < 0.726) {
playerDeck[playerDeckLength][0] = "Reverse Cycle";
} else if (randType < 0.792) {
playerDeck[playerDeckLength][0] = "+2 Cards";
} else if (randType < 0.858) {
playerDeck[playerDeckLength][0] = "+4 Cards";
} else if (randType < 0.924) {
playerDeck[playerDeckLength][0] = "Skip Turn";
} else if (randType < 1) {
playerDeck[playerDeckLength][0] = "Color Change";
}
//GENERATES RANDOM CARD COLOR
if (randColor < 0.25) {
playerDeck[playerDeckLength][1] = "Blue";
} else if (randColor < 0.5) {
playerDeck[playerDeckLength][1] = "Yellow";
} else if (randColor < 0.75) {
playerDeck[playerDeckLength][1] = "Red";
} else if (randColor < 1) {
playerDeck[playerDeckLength][1] = "Green";
}
//CHECKS IF CARD IS WILDCARD
if (playerDeck[playerDeckLength][0] == "+4 Cards") {
playerDeck[playerDeckLength][1] = "Wildcard";
} else if (playerDeck[playerDeckLength][0] == "+2 Cards") {
playerDeck[playerDeckLength][1] = "Wildcard";
} else if (playerDeck[playerDeckLength][0] == "Color Change") {
playerDeck[playerDeckLength][1] = "Wildcard";
}
playerDeckLength += 1;
} catch (Exception e) {
System.out.println("");
System.out.println("> An uncaught error occured!");
System.out.println("> Location: Card Generation");
}
System.out.println("Type: " + playerDeck[playerDeckLength][0] + "; Color: " +
playerDeck[playerDeckLength][1]);
}
} catch (Exception e) {
System.out.println("");
System.out.println("> An uncaught error occured!");
System.out.println("> Location: Player Deck Init");
}
} catch (Exception e) {
System.out.println("");
System.out.println("> An uncaught error occured!");
System.out.println("> Location: Main Output");
}
}
}
命令提示符:
> Deck:
> Cards Generated:
> An uncaught error occurred!
> Location: Card Generation
> An uncaught error occurred!
> Location: Player Deck Init
1楼
您正在初始化一个空的二维字符串数组。 该代码尝试访问未分配的索引,因此我认为程序可能会抛出IndexOutOfBounds异常
2楼
似乎是很多代码,但类/函数却很少;)
首先,尝试组织更好的代码,调试,修改和维护代码会更容易。我还邀请您阅读有关Java中的Exception和Exception处理的信息,您会发现每次使用Exception都会导致很多问题 !
当然,我们中的一些人将能够使您的程序正常工作,但是说实话,您只需要阅读更多内容,您就会成功:)
3楼
您不初始化数组,可能会出现“ IndexOutOfBounds”错误。
尝试使用以下方式初始化数组:“ String [] [] playerDeck = new String [7] [2];”。
另外,您还需要将您的支票从playerDeck[playerDeckLength][0] == "+4 Cards"
更改为if (playerDeck[playerDeckLength][0].equalsIgnoreCase("+4 Cards"))
4楼
问题是您初始化了一个空(二维)数组。 当您尝试访问它时,它将给出索引超出范围的异常。 如果知道大小,则必须以该大小启动它。
除此之外,请检查对您的问题的评论。 这应该可以帮助您自己解决这些问题。