Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java程序将整数转换为罗马数字时的运行时错误_Java_Indexoutofboundsexception - Fatal编程技术网

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

java程序将整数转换为罗马数字时的运行时错误,java,indexoutofboundsexception,Java,Indexoutofboundsexception,在我的面向对象编程课程中,我们被要求修改一个转换1到3999之间整数的程序。我们不允许使用数组或循环。无论如何,下面是我的代码,它没有我能识别的错误,但是有一些东西阻止了程序成功运行。以下是将随机数(让我们使用100)放入短语“in.nextInt()”时控制台上的显示: “输入一个介于1和3999之间的数字开始: 线程“main”java.lang.StringIndexOutOfBoundsException中的异常: 字符串索引超出范围:100 这条信息的大部分对我来说都是胡言乱语,但我真

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

“输入一个介于1和3999之间的数字开始:

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

这条信息的大部分对我来说都是胡言乱语,但我真的很难理解为什么当我的范围是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!");   

}

}
Scanner-in=新的扫描仪(System.in);
System.out.println(“输入一个介于1和3999之间的数字以开始:”);
int input=in.nextInt();
//我们希望避免0、负数和大于3999的数字
如果(输入<1 | |输入>3999);
系统输出打印(“无效数字!”);
System.out.println(“请输入一个介于1和3999之间的数字”);
//创建要在以后为不同情况定义的字符串
字符串romanOnes=(“”);
字符串romanTens=(“”);
字符串为(“”);
字符串=(“”);
//从输入中分离一、十、百和千
int ones1=输入%10;
int ones=ones1;
int tens1=输入%10;
if(时态1<10)
{tens1=input/10;}
其他的
{tens1=(tens1%100)-one;}
int-tens=tens1;
int hundreds1=输入%100;
如果(百分之一小于10)
{hundreds1=input/100;}
其他的
{hundreds1=(hundreds1%1000)-十位数;}
整数百=百分之一;
整数千分之一=输入%1000;
如果(千分之一小于4)
{000ands1=input/1000;}
其他的
{千分之一=(千分之一%10000)-百分之十;}
整数千=千1;
//“一”的不同格及其在罗马数字中的转换
如果(一=0)
{romanOnes=(“”);}
而(一=1);
{romanOnes=(“I”);}
而(一=2);
{romanOnes=(“II”);}
而(一=3);
{romanOnes=(“III”);}
而(1==4);
{romanOnes=(“IV”);}
而(一=5);
{romanOnes=(“V”);}
而(1==6);
{romanOnes=(“VI”);}
而(1==7);
{romanOnes=(“VII”);}
而(1==8);
{romanOnes=(“VIII”);}
而(1==9);
{romanOnes=(“IX”);}
//“十”的不同情况及其在罗马数字中的转换
如果(十=0)
{romanTens=(“”);}
而(十=1);
{romanTens=(“X”);}
而(十=2);
{romanTens=(“XX”);}
而(十=3);
{romanTens=(“XXX”);}
而(十=4);
{romanTens=(“XL”);}
而(十=5);
{romanTens=(“L”);}
而(十=6);
{romanTens=(“LX”);}
而(十=7);
{romanTens=(“LXX”);}
而(十=8);
{romanTens=(“LXXX”);}
而(十=9);
{romanTens=(“XC”);}
//“百”的不同格及其在罗马数字中的转换
如果(百分位==0)
{ROMAN=(“”);}
而(百=1);
{C=(“C”);}
而(百分之二),;
{ROMAN=(“CC”);}
而(百分之=3);
{ROMAN=(“CCC”);}
而(百分之四),;
{roman=(“CD”);}
而(百分之五),;
{roman=(“D”);}
而(百分之六),;
{ROMAN=(“DC”);}
而(百分之七),;
{ROMAN=(“DCC”);}
而(百分之八),;
{ROMAN=(“DCCC”);}
而(百分之九),;
{CM=(“CM”);}
//“千”的不同格及其在罗马数字中的转换
如果(千=0)
{ROMAN=(“”);}
而(千=1);
{roman=(“M”);}
而(千=2);
{roman=(“MM”);}
而(千=3);
{roman=(“MMM”);}
//连接所有字符串以生成完整的罗马数字
字符串罗马=(罗马千+罗马百+罗马十+罗马一);
//输出和再见消息
System.out.println(罗马数字中的输入+”为:“+罗马”;
System.out.println(“谢谢你,再见!”);
}
}

提前感谢您的帮助

不允许使用数组或循环?可能会被告知您必须在建筑物的一侧用山羊血绘制程序。无论如何,您需要使用调试器。此“*胡言乱语”消息表示您正试图访问某个位置(
索引
)中的字符大于字符串长度。出于调试目的,请将
包装在.nextInt()中;
包装在
if(in.hasNextInt)
语句中。您可能还需要发布整个
main
方法。“。。。当在短语“
in.nextInt()
”中输入一个随机数(让我们使用100)时,请解释您实际做了什么:通过控制台输入100,或将其作为
in.nextInt()
方法的参数。
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!");   

}

}