0

オブジェクト指向プログラミング コースの課題として、1 から 3999 までの整数を変換するプログラムを正しくするように求められました。配列やループを使用することは許可されていません。とにかく、以下は、識別できるエラーはありませんが、何かがプログラムの正常な実行を妨げている私のコードです。以下は、「in.nextInt()」というフレーズに乱数 (100 を使用しましょう) を入れたときのコンソールの表示です。

"開始するには、1 から 3999 までの数字を入力してください:

スレッド「メイン」の例外 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!");   

}

}

よろしくお願いいたします。

4

0 に答える 0