我的代码中存在此错误:
线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException: 70
在Main.main(Main.java:72)
我不知道为什么要这么做?请帮忙。我提前谢谢你。我在几个网站上经历了多个线程,但没有任何有效的方法。我已经为此工作了一段时间。我知道ArrayIndexOutOfBoundsException是什么,但是我一直没有成功
这是代码:
import java.util.Scanner;
import java.util.Random;
public class Main
{
public static void main (String[] args)
{
Scanner myScanner = new Scanner (System.in);
Random myRandom = new Random();
int answer;
int definitionSelected = 0;
String[] code = new String[68];
String[] definition = new String[68];
int[] listedDefinition = new int[4];
int[] definitionUsed = new int[68];
int i;
int Size = 68;
int Other = 4;
double[] alpha = new double[68];
int count = 0;
code[0] = "10-0" ; definition[0] = "Disappeared";
code[1] = "10-1" ; definition[1] = "Frequency Change";
code[2] = "10-2" ; definition[2] = "Negative";
code[3] = "10-3" ; definition[3] = "Stop Transmitting";
code[4] = "10-4" ; definition[4] = "Affirmative";
code[5] = "10-5" ; definition[5] = "Relay Message";
code[6] = "10-6" ; definition[6] = "Busy";
code[7] = "10-7" ; definition[7] = "Out Of Service";
code[8] = "10-8" ; definition[8] = "In Service";
code[9] = "10-9" ; definition[9] = "Repeat";
code[10] = "10-10" ; definition[10] = "Fight In Progress";
code[11] = "10-11" ; definition[11] = "Traffic Stop";
code[12] = "10-12" ; definition[12] = "Standby";
code[13] = "10-13" ; definition[13] = "Gun Shots";
code[15] = "10-15" ; definition[15] = "Subject en Route to station";
code[16] = "10-16" ; definition[16] = "Stolen Vehicle";
code[17] = "10-17" ; definition[17] = "Suspicious Person";
code[19] = "10-19" ; definition[19] = "Active Ride Along";
code[20] = "10-20" ; definition[20] = "location";
code[22] = "10-22" ; definition[22] = "Disregard";
code[23] = "10-23" ; definition[23] = "Arrived on scene";
code[25] = "10-25" ; definition[25] = "Do you have contact with ?";
code[26] = "10-26" ; definition[26] = "ETA";
code[27] = "10-27" ; definition[27] = "Drivers license check for valid";
code[28] = "10-28" ; definition[28] = "license Plate check";
code[29] = "10-29" ; definition[29] = "NCIC Warrant Check";
code[30] = "10-30" ; definition[30] = "Wanted Person";
code[31] = "10-31" ; definition[31] = "Armed Suspect";
code[32] = "10-32" ; definition[32] = "Request Backup, Code 1,2,3";
code[34] = "10-34" ; definition[34] = "Wanted Person";
code[35] = "10-35" ; definition[35] = "Not Wanted No Warrants";
code[38] = "10-38" ; definition[38] = "Suspicious Vehicle";
code[41] = "10-41" ; definition[41] = "Beginning Tour Of Duty";
code[42] = "10-42" ; definition[42] = "Ending Tour Of Duty";
code[43] = "10-43" ; definition[43] = "Information About ?";
code[46] = "10-46" ; definition[46] = "Attempt Of Suicide";
code[49] = "10-49" ; definition[49] = "Drag Racing";
code[50] = "10-50" ; definition[50] = "vehicle Accident";
code[51] = "10-51" ; definition[51] = "Request Towing Service";
code[52] = "10-52" ; definition[52] = "Request EMS";
code[53] = "10-53" ; definition[53] = "Request Fire Department";
code[54] = "10-54" ; definition[54] = "Hit And Run";
code[55] = "10-55" ; definition[55] = "Intoxicated Driver";
code[56] = "10-56" ; definition[56] = "Intoxicated Pedestrian";
code[60] = "10-60" ; definition[60] = "Assault By An Animal";
code[61] = "10-61" ; definition[61] = "Bicycle Theft";
code[62] = "10-62" ; definition[62] = "Kidnapping";
code[63] = "10-63" ; definition[63] = "Radio Check";
code[64] = "10-64" ; definition[64] = "Sexual Assualt";
code[65] = "10-65" ; definition[65] = "Armed Robbery";
code[66] = "10-66" ; definition[66] = "Reckless Driver";
code[67] = "10-67" ; definition[67] = "Fire";
code[70] = "10-70" ; definition[70] = "Foot Pursuit";
code[71] = "10-71" ; definition[71] = "Request Supervisor On Scene";
code[72] = "10-72" ; definition[72] = "Administrative Assistance";
code[73] = "10-73" ; definition[73] = "Advise Status";
code[74] = "10-74" ; definition[74] = "Negative";
code[76] = "10-76" ; definition[76] = "Prowler";
code[80] = "10-80" ; definition[80] = "Vehicle Pursuit";
code[85] = "10-85" ; definition[85] = "Delay Due To";
code[86] = "10-86" ; definition[86] = "Any Traffic For me?";
code[88] = "10-88" ; definition[88] = " Requesting Deputy Chief (Department)";
code[89] = "10-89" ; definition[89] = "Requesting Chief (Department)";
code[90] = "10-90" ; definition[90] = "Warning";
code[91] = "10-91" ; definition[91] = "Unnecessary use of Radio";
code[93] = "10-93" ; definition[93] = "Bank Alarm";
code[97] = "10-97" ; definition[97] = "In route";
code[99] = "10-99" ; definition[99] = "Officer In Distress EXTREME EMERGENCY ONLY";
do
{
answer = myRandom.nextInt(68);
System.out.println("\nCode Is : " + code [answer] + "\n");
for (i = 0;i<Size;i++) {
int j;
do
{
j = myRandom.nextInt(68);
}
while (definitionUsed[j] != 0 || j == answer);
definitionUsed[j] = 1;
listedDefinition[i] = j;
}
listedDefinition[myRandom.nextInt(4)] = answer;
for (i = 0;i<Other;i++)
{
System.out.println((i + 1) + " - " +
definition[listedDefinition[i]]);
}
System.out.print("\nWhat is The definition? (Enter 0 to Stop) ");
definitionSelected = myScanner.nextInt();
if (definitionSelected != 0)
{
if (listedDefinition[definitionSelected - 1] == answer)
System.out.println("That's It Good Job");
}
else
{
System.out.println("Sorry, The answer is " + definition +".");
}
}
while (definitionSelected != 0);
}
}
答案 0 :(得分:0)
您必须确定初始数组大小。
在您的代码中,您拥有:
code[99] = "10-99"
和
definition[99] = "Officer In Distress EXTREME EMERGENCY ONLY";
在两种情况下,您都试图定位99
的位置,这意味着您至少有100
个元素(因为100
个元素的位置是从0
到99
。
因此,您必须在以下几行中将初始大小从68
更改为100
:
String[] code = new String[68];
String[] definition = new String[68];
int[] listedDefinition = new int[4];
int[] definitionUsed = new int[68];
double[] alpha = new double[68];
收件人:
String[] code = new String[100];
String[] definition = new String[100];
int[] listedDefinition = new int[100];
int[] definitionUsed = new int[100];
double[] alpha = new double[100];
答案 1 :(得分:0)
您已经定义了一个大小为68的数组,并且您正在尝试使用超出此大小的数组。由于数组的大小不能动态增加。因此,我们得到了例外,即数组索引超出范围。如果要动态更改大小,请使用ArrayList。