我正在制作一个阿拉伯数字到罗马数字的转换器并收到错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at arab2Roman.arabicToRoman(arab2Roman.java:27)
at arab2Roman.main(arab2Roman.java:178)
任何建议/反馈将不胜感激。
public class arab2Roman {
public static String arabicToRoman(int Arabic) {
if (Arabic==-1) {
System.out.println("Program exiting...");
System.exit(0);
} else if (Arabic<=0) {
return "Invalid Option: Must be greater than 0";
} else if (Arabic>=4000) {
return "Invalid Option: Must be under 4000.";
} else {
String banana = new String();
String temp = Integer.toString(Arabic);
int[] arabArray = new int[temp.length()];
for (int i = 0; i < temp.length(); i++) {
arabArray[i] = temp.charAt(i) - '0';
}
int o = 0;
char[] romanArray = new char[o];
for (int i=0;i<temp.length();) {
if (temp.length()==4) {
if (arabArray[0]==3) {
for (;o<3;o++) {
romanArray[o] = 'M';
}
} else if (arabArray[0]==2) {
for (;o<2;o++) {
romanArray[o] = 'M';
}
} else if (arabArray[0]==1) {
romanArray[0]= 'M';
}
if (arabArray[1]==9) {
romanArray[o]='C';
o++;
romanArray[o]='M';
o++;
} else if (arabArray[1]==8) {
romanArray[o]='D';
o++;
for (int p=o;o<p+3;o++) {
romanArray[o]='C';
}
} else if (arabArray[1]==7) {
romanArray[o]='D';
o++;
for (int p=o;o<p+2;o++) {
romanArray[o]='C';
}
} else if (arabArray[1]==6) {
romanArray[o]='D';
o++;
romanArray[o]='C';
o++;
} else if (arabArray[1]==5) {
romanArray[o]='D';
o++;
} else if (arabArray[1]==4) {
romanArray[o]='C';
o++;
romanArray[o]='D';
o++;
} else if (arabArray[1]==3) {
for (int p=o;o<p+3;o++) {
romanArray[o]='C';
}
} else if (arabArray[1]==2) {
for (int p=o;o<p+2;o++) {
romanArray[o]='C';
}
} else if (arabArray[1]==1) {
romanArray[o]='C';
o++;
}
if (arabArray[2]==9) {
romanArray[o]='X';
o++;
romanArray[o]='C';
o++;
} else if (arabArray[2]==8) {
romanArray[o]='L';
o++;
for (int p=o;o<p+3;o++) {
romanArray[o]='X';
}
} else if (arabArray[2]==7) {
romanArray[o]='L';
o++;
for (int p=o;o<p+2;o++) {
romanArray[o]='X';
}
} else if (arabArray[2]==6) {
romanArray[o]='L';
o++;
romanArray[o]='X';
o++;
} else if (arabArray[2]==5) {
romanArray[o]='L';
o++;
} else if (arabArray[2]==4) {
romanArray[o]='X';
o++;
romanArray[o]='L';
o++;
} else if (arabArray[2]==3) {
for (int p=o;o<p+3;o++) {
romanArray[o]='X';
}
} else if (arabArray[2]==2) {
for (int p=o;o<p+2;o++) {
romanArray[o]='X';
}
} else if (arabArray[2]==1) {
romanArray[o]='X';
o++;
}
if (arabArray[3]==9) {
romanArray[o]='I';
o++;
romanArray[o]='X';
o++;
} else if (arabArray[3]==8) {
romanArray[o]='V';
o++;
for (int p=o;o<p+3;o++) {
romanArray[o]='I';
}
} else if (arabArray[3]==7) {
romanArray[o]='V';
o++;
for (int p=o;o<p+2;o++) {
romanArray[o]='I';
}
} else if (arabArray[3]==6) {
romanArray[o]='V';
o++;
romanArray[o]='I';
o++;
} else if (arabArray[3]==5) {
romanArray[o]='V';
o++;
} else if (arabArray[3]==4) {
romanArray[o]='I';
o++;
romanArray[o]='V';
o++;
} else if (arabArray[3]==3) {
for (int p=o;o<p+3;o++) {
romanArray[o]='I';
}
} else if (arabArray[3]==2) {
for (int p=o;o<p+2;o++) {
romanArray[o]='I';
}
} else if (arabArray[3]==1) {
romanArray[o]='I';
o++;
}
banana = new String(romanArray);
i++;
return banana;
}
}
}
return null;
}
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner dog = new Scanner(System.in);
for (int i=0;i==0;) {
System.out.println("Enter a number (1-3996) to convert to Roman Numberals(enter -1 to exit):");
int Arabic = dog.nextInt();
System.out.println(arabicToRoman(Arabic));
}
}
}