java程序中的运行时错误将整数转换为罗马数字

时间:2013-09-28 18:52:21

标签: java indexoutofboundsexception

作为我面向对象编程课程的一项任务,我们被要求修改一个转换1到3999之间整数的程序。我们不允许使用数组或循环。无论如何,下面是我的代码,它没有我能辨别的错误,但有些东西阻止程序成功运行。以下是当一个随机数(让我们使用100)放入短语“in.nextInt()”时控制台上的显示:

“输入1到3999之间的数字开始:

  

线程“main”中的异常java.lang.StringIndexOutOfBoundsException:   字符串索引超出范围:100

at java.lang.String.substring(String.java:1934)
at java.util.Scanner.buildIntegerPatternString(Scanner.java:447)
at java.util.Scanner.integerPattern(Scanner.java:467)
at java.util.Scanner.nextInt(Scanner.java:2091)
at IntegerToRomanNumeral.main(IntegerToRomanNumeral.java:13)"

这条消息大部分都是胡言乱语,但我真的很难理解为什么在我的范围是1-3999时数字100被解释为“超出范围”。

Scanner in = new Scanner (System.in);
System.out.println("Enter a number between 1 and 3999 to begin: ");
int input = in.nextInt( );

    //we want to avoid 0, negative numbers & numbers greater than 3999
if (input < 1 || input > 3999);
    System.out.print("Invalid Number!");
    System.out.println("Please enter a number between 1 and 3999.");

        //creating strings to be defined for the different cases later
    String romanOnes = ("");
    String romanTens = ("");
    String romanHundreds = ("");
    String romanThousands = ("");

    //separating the ones, tens, hundreds & thousands from within the input
int ones1 = input % 10;
    int ones = ones1;

    int tens1 = input % 10;
        if (tens1 < 10)
        { tens1 = input / 10; }
        else
        { tens1 = (tens1 % 100) - ones; }

        int tens = tens1;

    int hundreds1 = input % 100;
        if (hundreds1 < 10)
        { hundreds1 = input / 100; }
        else
        { hundreds1 = (hundreds1 % 1000) - tens - ones; }

        int hundreds = hundreds1;

    int thousands1 = input % 1000;
        if (thousands1 < 4)
        { thousands1 = input / 1000; }
        else
        { thousands1 = (thousands1 % 10000) - hundreds - tens - ones; }

        int thousands = thousands1;

        // different cases for the "ones" and their conversion in to Roman numerals
    if (ones == 0)
        {romanOnes = ("");}
        while (ones == 1);
            {romanOnes = ("I");}
        while (ones == 2);
            {romanOnes = ("II");}   
        while (ones == 3);
            {romanOnes = ("III");}
        while (ones == 4);
            {romanOnes = ("IV");}
        while (ones == 5);                  
                            {romanOnes = ("V");}
        while (ones == 6);
            {romanOnes = ("VI");}
        while (ones == 7);
            {romanOnes = ("VII");}
        while (ones == 8);
            {romanOnes = ("VIII");}
        while (ones == 9);
            {romanOnes = ("IX");}   

            // different cases for the "tens" and their conversion in to Roman numerals
        if (tens == 0)
            {romanTens = ("");}
        while (tens == 1);
            {romanTens = ("X");}
        while (tens == 2);
            {romanTens = ("XX");}   
        while (tens == 3);
            {romanTens = ("XXX");}
        while (tens == 4);
            {romanTens = ("XL");}
        while (tens == 5);
            {romanTens = ("L");}
        while (tens == 6);
            {romanTens = ("LX");}
        while (tens == 7);
            {romanTens = ("LXX");}
        while (tens == 8);
            {romanTens = ("LXXX");}
        while (tens == 9);
            {romanTens = ("XC");}   

            // different cases for the "hundreds" and their conversion in to Roman numerals
        if (hundreds == 0)
            {romanHundreds = ("");}
        while (hundreds == 1);
            {romanHundreds = ("C");}
        while (hundreds == 2);
            {romanHundreds = ("CC");}   
        while (hundreds == 3);
            {romanHundreds = ("CCC");}
        while (hundreds == 4);
            {romanHundreds = ("CD");}
        while (hundreds == 5);
            {romanHundreds = ("D");}
        while (hundreds == 6);
            {romanHundreds = ("DC");}
        while (hundreds == 7);
            {romanHundreds = ("DCC");}
        while (hundreds == 8);
            {romanHundreds = ("DCCC");}
        while (hundreds == 9);
            {romanHundreds = ("CM");}   

            // different cases for the "thousands" and their conversion in to Roman numerals
        if (thousands == 0)
            {romanThousands = ("");}
        while (thousands == 1);
            {romanThousands = ("M");}
        while (thousands == 2);
            {romanThousands = ("MM");}  
        while (thousands == 3);
            {romanThousands = ("MMM");} 

            //Concatenating all strings to produce the complete Roman numeral
        String roman = (romanThousands + romanHundreds + romanTens + romanOnes);

            //output & goodbye message
        System.out.println(input + "in roman numerals is: " + roman);
        System.out.println("Thank you and goodbye!");   

}

}

提前感谢您的帮助

0 个答案:

没有答案