0

我有 3 个班级,但我认为问题出现在 Main 和 Roman Numerals 班级之间。. . 还将包括类变量。. .

import java.util.Scanner;

public class Main{

public static void main (String[] args){
    System.out.println("Press 1 for RPS, 2 for Flowchart, 3 for Roman Numerals, 4 for Ascendant");
    Scanner Selection = new Scanner(System.in);
    Variables sel = new Variables(); 
    sel.run = Selection.nextLine();
        {
        if (sel.run.equals("1")){
        System.out.println("You have chosen and initialized 1!");
        System.out.println("\nAttempting to Connect to RPS Class\n");
        RPS JackEnPoy = new RPS();
        JackEnPoy.RockPaperScissors();
        }
        else if (sel.run.equals("2")){
        System.out.println("You have chosen and initialized 2!");
        System.out.println("\nAttempting to Connect to Flowchart Class\n");
        Flowchart chart = new Flowchart();
        chart.Flow();
        }
        else if (sel.run.equals("3")){
        System.out.println("You have chosen and initialized 3!");
        System.out.println("\nAttempting to Connect to RomanNumerals Class\n");
        RomanNumerals rn = new RomanNumerals();
        rn.Numeral();
        }
        else if (sel.run.equals("4")){
        System.out.println("You have chosen and initialized 4!");
        System.out.println("\nAttempting to Connect to Ascending Class\n");
        //Ascending aorder = new Ascending();
        //aorder.Ascendant();

        }
        }
    }
}

以及其他类的类变量

class Variables{

String run;
//RPS
String guide;
String RW = "Rock Smashes Scissors!";
String PW = "Paper Covers Rock!";
String SW = "Scissors Shreds Paper!";
String T = "Impasse!";
//Flowchart
int x;
int y;
int z;
boolean YN;
}

这是我目前一直在尝试使用的罗马数字,而 RPS(Rock Paper Scissors)类和流程图类工作正常

import java.util.Scanner;
import javax.swing.JOptionPane;
public class RomanNumerals{

public static void Numeral()
{
//Declarations
String num = " ", roman = " ";
char sen = num.charAt(0), hachi = num.charAt(1), jyu = num.charAt(2), ichi = num.charAt(3);
int convert;

//String from num to Integer
convert = Integer.parseInt(num);
num = JOptionPane.showInputDialog("Convert from Whole Numbers to Roman Numerals(Maximum:3000)");


//'if' more than 3k then terminate 'else' attempt conversion
if (convert > 3000)
{
//Invalid Message with WARNING_MESSAGE dialog
JOptionPane.showMessageDialog (null, "Input had exceeded the maximum of 3000", "RomanNumerals3000", JOptionPane.WARNING_MESSAGE);
}
else
{
if (ichi == '1')//0001
roman += "I";
if (ichi == '2')//0002
roman += "II";
if (ichi == '3')//0003
roman += "III";
if (ichi == '4')//0004
roman += "IV";
if (ichi == '5')//0005
roman += "V";
if (ichi == '6')//0006
roman += "VI";
if (ichi == '7')//0007
roman += "VII";
if (ichi == '8')//0008
roman += "VIII";
if (ichi == '9')//0009
roman += "IX";
    if (jyu == '1')//0010
    roman += "X";
    if (jyu == '2')//0020
    roman += "XX";
    if (jyu == '3')//0030
    roman += "XXX";
    if (jyu == '4')//0040
    roman += "XL";
    if (jyu == '5')//0050
    roman += "L";
    if (jyu == '6')//0060
    roman += "LX";
    if (jyu == '7')//0070
    roman += "LXX";
    if (jyu == '8')//0080
    roman += "LXXX";
    if (jyu == '9')//0090
    roman += "XC";
        if (hachi == '1')//0100
        roman += "C";
        if (hachi == '2')//0200
        roman += "CC";
        if (hachi == '3')//0300
        roman += "CCC";
        if (hachi == '4')//0400
        roman += "CD";
        if (hachi == '5')//0500
        roman += "D";
        if (hachi == '6')//0600
        roman += "DC";
        if (hachi == '7')//0700
        roman += "DCC";
        if (hachi == '8')//0800
        roman += "DCCC";
        if (hachi == '9')//0900
        roman += "CM";
                if (sen == '1')//1000
                roman += "M";
                if (sen == '2')//2000
                roman += "MM";
                if (sen == '3')//3000
                roman += "MMM";
JOptionPane.showMessageDialog(null, "Whole Number Form = " + num + "\nRoman Numeral Form = " + roman, "Converted!!!", JOptionPane.INFORMATION_MESSAGE,null);
}
System.exit(0);
}
}

输出是这样的。.

3
You have chosen and initialized 3!

Attempting to Connect to RomanNumerals Class

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
    at java.base/java.lang.String.charAt(String.java:1512)
    at RomanNumerals.Numeral(RomanNumerals.java:9)
    at Main.main(Main.java:27)
Command execution failed.```
I'm still new here and can't figure out on why am I getting this error after initiating 3 as in the RomanNumerals class. . 
4

2 回答 2

0

所以你的问题看起来像在这里:

String num = " ", roman = " ";

所以在上面你已经初始化num为一个字符串,其中包含一个字符,一个空格。

char sen = num.charAt(0), hachi = num.charAt(1), jyu = num.charAt(2), ichi = num.charAt(3);

在上面的下一行中,您要求输入第一个(char(0)因为在 Java 中字符串字符索引是从零开始的)、第二个 ( char(1))、第三个 ( char(2)) 和第四个 ( char(3)) 字符。

这失败了,因为字符串只有一个字符。

此外,仅仅因为代码编译并不意味着它会成功运行。

于 2021-10-29T18:34:58.757 回答
0
char sen = num.charAt(0), hachi = num.charAt(1), jyu = num.charAt(2), ichi = num.charAt(3);

如果字符串长度少于 4 个字符,这将崩溃,因为它不知道 num.charAt() 应该返回什么。您想重写它,以便仅在字符串的长度足够长时才获取字符。

于 2021-10-29T18:33:26.893 回答