Java 无法找出此程序的输出不正确的原因

Java 无法找出此程序的输出不正确的原因,java,Java,我正在创建一个程序,将用户输入的数字转换成英语(例如56=56)。但是,我的输出完全不正确。我试过玩弄它,但我不确定到底出了什么问题。这就是我正在使用的代码!数字7输出“0千0”,数字86输出“0千8”,数字7556输出错误消息: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 53 out of bounds for length 8 at SayingNumber

我正在创建一个程序,将用户输入的数字转换成英语(例如56=56)。但是,我的输出完全不正确。我试过玩弄它,但我不确定到底出了什么问题。这就是我正在使用的代码!数字7输出“0千0”,数字86输出“0千8”,数字7556输出错误消息:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 53 out of bounds for length 8
    at SayingNumbers.twoDigitToStr(SayingNumbers.java:51)
    at SayingNumbers.threeDigitToStr(SayingNumbers.java:70)
    at SayingNumbers.numToStr(SayingNumbers.java:120)
    at SayingNumbers.main(SayingNumbers.java:135)
这就是我一直在使用的代码!如有任何故障排除方面的帮助,将不胜感激

//This program takes a user inputted number and writes out the corresponding words in English
import java.util.Scanner;
public class SayingNumbers {
    //This returns the English word for numbers 0 - 9
    public static String oneDigitToStr(int num) {
        switch (num) {
            case 0 : return "zero";
                case 1 : return "one";
                case 2 : return "two";
                case 3 : return "three";
                case 4 : return "four";
                case 5 : return "five";
                case 6 : return "six";
                case 7 : return "seven";
                case 8 : return "eight";
                case 9 : return "nine";
        }
        return"";
    }
    //This returns the English words for numbers 10 - 99
    public static String twoDigitToStr(int num) {
        //This returns the English word for numbers 10 - 19
        switch(num){
            case 10 : return "ten";
            case 11 : return "eleven";
            case 12 : return "tweleve";
            case 13 : return "thirteen";
            case 14 : return "fourteen";
            case 15 : return "fifteen";
            case 16 : return "sixteen";
            case 17 : return "seventeen";
            case 18 : return "eighteen";
            case 19 : return "nineteen";
        }
        //This returns the English word for multiples of 10 up until 90.
        String tens[] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
        int onesPlace;
        int tensPlace;
        int i;
        i = num;
        onesPlace = i%10; //Finds ones place
        i /= 10;
        tensPlace = i; //Finds tens place
        if(tensPlace == 0) {
            return oneDigitToStr(onesPlace); //Calls oneDigitToStr for single digit numbers
        }
        if(onesPlace == 0) {
            return tens[tensPlace - 2]; //Multiples of 10
        }
        //Returns all other two digit numbers > 20
        return tens[tensPlace - 2] + " " + oneDigitToStr(onesPlace);
    }
    //Returns the English words for numbers 100-999
    public static String threeDigitToStr (int num) {
        String hundreds = "hundred";
        int hundredsPlace;
        int tensPlace;
        int onesPlace;
        int i;
        i = num;
        onesPlace = i % 100;
        hundredsPlace = i / 100;
        i = num;
        i = i - hundredsPlace;
        tensPlace = i / 10;
        if (hundredsPlace == 0) {
            return twoDigitToStr(tensPlace);
        }
        i = num - hundredsPlace;
        return oneDigitToStr(hundredsPlace) + " hundred " + twoDigitToStr(i);
    }
    //Returns the English words for numbers 1000+ (within int parameters)
    public static String numToStr (int num) {
        String userNum = Integer.toString(num);
        int billions;
        int millions;
        int thousands;
        int hundredsPlace;
        int tensPlace;
        int onesPlace;
        int i = num;
        int n = userNum.length();
        if (n > 9) {
            if (n < 12) {
                n = 12 - n;
                while (n > 0) {
                    userNum = "0" + userNum;
                    n--;
                }
            }
            billions = Integer.parseInt(userNum.substring(0,3));
            millions = Integer.parseInt(userNum.substring(3,6));
            thousands = Integer.parseInt(userNum.substring(6,9));
            hundredsPlace = Integer.parseInt(userNum.substring(9,12));
            return threeDigitToStr(billions) + " billion " + threeDigitToStr(millions) + " million " + threeDigitToStr(thousands) + " thousand " + threeDigitToStr(hundredsPlace);
        }
        else if (n > 6 && n <= 9) {
            if (n < 9) {
                n = 9 - n;
                while (n > 0) {
                    userNum = "0" + userNum;
                    n--;
                }
            }
            millions = Integer.parseInt(userNum.substring(0,3));
            thousands = Integer.parseInt(userNum.substring(3,6));
            hundredsPlace = Integer.parseInt(userNum.substring(6,9));
            return threeDigitToStr(millions) + " million " + threeDigitToStr(thousands) + " thousand " + threeDigitToStr(hundredsPlace);
        }
        else if (n > 3) {
            if (n < 6) {
                n = 6 - n;
                while (n > 0) {
                    userNum = "0" + userNum;
                    n--;
                } 
            }
            thousands = Integer.parseInt(userNum.substring(0,3));
            hundredsPlace = Integer.parseInt(userNum.substring(3,6));
            return threeDigitToStr(thousands) + " thousand " + threeDigitToStr(hundredsPlace);
        } 
        else {
            return threeDigitToStr(Integer.parseInt(userNum));
        }
        return "";
    }
    //This gets user input
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int numEntered;
        while(true) {
            System.out.print("Enter an integer to pronounce (any negative value to exit): ");
            numEntered = scnr.nextInt();
            if (numEntered < 0) {
                break;
            }
            System.out.println(numEntered);
            String numToString = numToStr(numEntered);
            System.out.println(numToString);
            }
            System.out.println("kthxbye!");
    }
}
//此程序获取用户输入的数字,并用英语写出相应的单词
导入java.util.Scanner;
公开课上说的数字{
//这将返回数字0-9的英文单词
公共静态字符串onedigittost(int num){
开关(num){
案例0:返回“零”;
案例1:返回“一”;
案例2:返回“二”;
案例3:返回“三”;
案例四:返回“四”;
案例5:返回“五”;
案例6:返回“六”;
案例7:返回“七”;
案例8:返回“八”;
案例9:返回“九”;
}
返回“”;
}
//这将返回数字10-99的英文单词
公共静态字符串twodigittost(int num){
//这将返回数字10-19的英文单词
开关(num){
案例10:返回“十”;
案例11:返回“十一”;
案例12:返回“tweleve”;
案例13:返回“十三”;
案例14:返回“十四”;
案例15:返回“十五”;
案例16:返回“十六”;
案例17:返回“十七”;
案例18:返回“十八”;
案例19:返回“十九”;
}
//这将返回10到90的倍数的英文单词。
字符串十[]={“二十”、“三十”、“四十”、“五十”、“六十”、“七十”、“八十”、“九十”};
int onesPlace;
int tensPlace;
int i;
i=num;
onesPlace=i%10;//找到自己的位置
i/=10;
tensPlace=i;//查找十位
if(tensPlace==0){
返回OneDigitOstr(onesPlace);//调用OneDigitOstr获取单个数字
}
如果(onesPlace==0){
返回十[tensPlace-2];//10的倍数
}
//返回所有其他大于20的两位数
返回十位[tensPlace-2]+“”+onedigittost(onesPlace);
}
//返回数字100-999的英文单词
公共静态字符串三位数(int num){
字符串数百=“百”;
国际百位;
int tensPlace;
int onesPlace;
int i;
i=num;
onesPlace=i%100;
百位=i/100;
i=num;
i=i-百位;
tensPlace=i/10;
如果(百分位==0){
返回TwoDigittost(tensPlace);
}
i=数量-百位;
返回一位数(百位数)+“百位数”+两位数(一位数);
}
//返回数字1000+的英文单词(在int参数内)
公共静态字符串numtost(int num){
字符串userNum=Integer.toString(num);
十亿整数;
百万整数;
整数千;
国际百位;
int tensPlace;
int onesPlace;
int i=num;
int n=userNum.length();
如果(n>9){
if(n<12){
n=12-n;
而(n>0){
userNum=“0”+userNum;
n--;
}
}
十亿=整数.parseInt(userNum.substring(0,3));
数百万=整数.parseInt(userNum.substring(3,6));
数千=整数.parseInt(userNum.substring(6,9));
hundredplace=Integer.parseInt(userNum.substring(9,12));
返回三位数(十亿)+“十亿”+三位数(百万)+“百万”+三位数(千)+“千”+三位数(百位);
}
否则如果(n>6&&n 0){
userNum=“0”+userNum;
n--;
}
}
数百万=整数.parseInt(userNum.substring(0,3));
数千=整数.parseInt(userNum.substring(3,6));
hundredplace=Integer.parseInt(userNum.substring(6,9));
返回三位数(百万)+“百万”+三位数(千)+“千”+三位数(百位);
}
否则,如果(n>3){
if(n<6){
n=6-n;
而(n>0){
userNum=“0”+userNum;
n--;
} 
}
数千=整数.parseInt(userNum.substring(0,3));
hundredplace=Integer.parseInt(userNum.substring(3,6));
返回三位数(千)+“千”+三位数(百位);
} 
否则{
返回threedigitstr(Integer.parseInt(userNum));
}
返回“”;
}
//这将获取用户输入
公共静态void main(字符串[]args){
扫描仪scnr=新扫描仪(System.in);
国际货币基金组织;
while(true){
System.out.print(“输入一个要发音的整数(任何要退出的负值):”;
numEntered=snr.nextInt();
如果(数值小于0){
打破
}
System.out.println(数字);
字符串numToString=numToString(numEntered);
System.out.println(numToString);
}
System.out.println(“kthxbye!”);
}
}

在您的代码中,存在多个问题,但在一行中,您的逻辑失败,并且该行位于方法
ThreeDigitOstr中(
最后一行,即返回语句有
i<
    return oneDigitToStr(hundredsPlace) + " hundred " + twoDigitToStr(i);
    return oneDigitToStr(hundredsPlace) + " hundred " + twoDigitToStr(onesPlace);