gpt4 book ai didi

java - 我的程序出现错误,我不知道如何修复

转载 作者:行者123 更新时间:2023-12-02 01:37:45 26 4
gpt4 key购买 nike

所以我正在尝试用 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

最佳答案

您正在初始化一个空的二维字符串数组。该代码尝试访问未分配的索引,因此我认为该程序可能会抛出 IndexOutOfBounds 异常

关于java - 我的程序出现错误,我不知道如何修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54982648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com