我正在制作阿拉伯语到罗马数字转换器并收到错误:
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));
}
}
}
答案 0 :(得分:2)
这里有很多东西可以改进/简化,但看起来主要问题是:
int o = 0;
char[] romanArray = new char[o];
这是创建一个大小为0的数组,因此romanArray[0]
进一步向下越界。
答案 1 :(得分:0)
这是我最终得到的算法。
import java.util.HashMap;
public class SimpleRomanConverter {
private static final HashMap<Integer, String> romanSymbolsMap = createRomanMap();
private static HashMap<Integer, String> createRomanMap() {
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "I");
map.put(4, "IV");
map.put(5, "V");
map.put(9, "IX");
map.put(10, "X");
map.put(40, "XL");
map.put(50, "L");
map.put(90, "XC");
map.put(100, "C");
map.put(400, "CD");
map.put(500, "D");
map.put(900, "CM");
map.put(1000, "M");
return map;
}
public String convert(int number) throws RomanNumberOutOfRangeException {
if (number < 0 || number > RomanNumbersInfo.MAX_ROMAN_ALLOWED)
throw new RomanNumberOutOfRangeException();
if(number == 0)
return "";
return find(number);
}
private String find(int number) {
if(romanSymbolsMap.containsKey(number))
return romanSymbolsMap.get(number);
int nearest = number;
while (!romanSymbolsMap.containsKey(nearest) && nearest != 0)
nearest--;
return romanSymbolsMap.get(nearest) + find(number - nearest);
}
}