Java 是否可以保存迭代字符串时获取的单个字符值?

Java 是否可以保存迭代字符串时获取的单个字符值?,java,Java,Java学生正在研究罗马计算器问题。我试图将给定的罗马数字字符串通过for循环,并将每个数字保存为字符,以便在下面的方法中将其转换为整数值。有没有办法将每个字符的值保存到变量或数组中,并在下一个方法中调用它来为其赋值?以下是我迄今为止所做的: int getOperand(int which) { System.out.println("Enter operand " + which); boolean invalid_operand; do { inv

Java学生正在研究罗马计算器问题。我试图将给定的罗马数字字符串通过for循环,并将每个数字保存为字符,以便在下面的方法中将其转换为整数值。有没有办法将每个字符的值保存到变量或数组中,并在下一个方法中调用它来为其赋值?以下是我迄今为止所做的:

int getOperand(int which) {
    System.out.println("Enter operand " + which);
    boolean invalid_operand;

    do {
        invalid_operand = false;

        String operand = keyboard.nextLine();
        int length = operand.length();
        if (operand.length() == 0)
            invalid_operand = true;

        for (int i = 0; i < length; i++) {
            char c = operand.charAt(i);

            if (c != 'M' || c != 'D' || c != 'C' || c != 'L' || c != 'X'
                    || c != 'V' || c != 'I') {
                invalid_operand = true;
                System.out.println("Invalid Numberal!");
            }
            char numeral = operand.charAt(i);
        }
    } while (invalid_operand);
     return which;
}
int getOperand(int which){
System.out.println(“输入操作数”+which);
布尔操作数无效;
做{
无效的操作数=false;
字符串操作数=keyboard.nextLine();
int length=操作数.length();
if(操作数.长度()==0)
无效的操作数=真;
for(int i=0;i
列出数字=新的ArrayList();
for(int i=0;i
借用发布的另一个答案,并对您的方法进行进一步修改,我相信您可以按照我在下面发布的任何一种方式来做您要求的事情(存储输入的字符以供其他方法使用)。第一个返回与有效字符对应的整数列表。第二个则以字符形式返回此列表

但是,请注意,我怀疑这两种方法是否都是解决问题的方法。看来你可能已经得到了一个模板,并将填补空白。这是真的吗?如果我认为方法的返回类型(int)是给定模板的一部分是正确的,那么您不应该更改它。在我看来,这个方法应该取一个操作数(例如CLII,表示152的罗马数字)并返回一个int(在本例中是152)。如果我在这一点上错了,那么我需要进一步澄清,以了解这种方法实际上应该做什么

名称“getOperator”似乎只表示读取用户输入并存储它以供将来使用,但为什么返回类型是int?如果预期的输入是一系列字符,int的返回类型会向我建议该方法将输入的罗马数字转换为整数(整数而不是整数)。我在这里使用integer一词来指代正在发生的事情:将罗马数字转换为整数(如整数152),而不是整数(一个Java类,您可以选择使用它来帮助实现这一点)

也许您可以澄清该方法应该做什么,该方法的一部分是否从一开始就提供给了您,如果是的话,具体提供给您的部分以及您自己添加的内容

我已经说过了,下面是我上面提到的两种方法

方法1(作为整数):

/*将方法的返回类型更改为ArrayList,以便
如果从另一个方法调用该方法,则检索包含
只是与所需字符对应的整数*/
ArrayList GetOperator(int-which){
System.out.println(“输入操作数”+which);
布尔操作数无效;
ArrayList numbers=新的ArrayList();
做{
无效的操作数=false;
字符串操作数=keyboard.nextLine();
int length=操作数.length();
if(操作数.长度()==0)
无效的操作数=真;
for(int i=0;i
方法2(以字符形式):

/*将方法的返回类型更改为ArrayList,以便
如果从另一个方法调用该方法,则检索包含
只是与所需字符对应的字符*/
ArrayList GetOperator(int-which){
System.out.println(“输入操作数”+which);
布尔操作数无效;
ArrayList numbers=新的ArrayList();
做{
无效的操作数=false;
字符串操作数=keyboard.nextLine();
int length=操作数.length();
if(操作数.长度()==0)
无效的操作数=真;
for(int i=0;iList<Integer> numerals = new ArrayList<Integer>();
for (int i = 0; i < length; i++) {
    char c = operand.charAt(i);

    if (c != 'M' && c != 'D' && c != 'C' && c != 'L' && c != 'X'
            && c != 'V' && c != 'I') {
        invalid_operand = true;
        System.out.println("Invalid Numeral!");
    } else {
        // Use lists to save any numerals. You can access values
        // in the list just like in an array
        // Added the else in order to stop adding invalid numerals
        // to the list, as suggested by @Tom.
        numerals.add(c); 
    }   
}
// Do something with the list after it has all the valid numerals
 /* changed the return type of the method to ArrayList<Integer> so that when 
    you call the method from another, you retrieve an ArrayList containing 
    just the Integers corresponding to the chars you want */

ArrayList<Integer> getOperand(int which) {
    System.out.println("Enter operand " + which);
    boolean invalid_operand;
    ArrayList<Integer> numerals = new ArrayList<Integer>();

    do {
        invalid_operand = false;

        String operand = keyboard.nextLine();
        int length = operand.length();
        if (operand.length() == 0)
            invalid_operand = true;

        for (int i = 0; i < length; i++) {
            char c = operand.charAt(i);

            if (c != 'M' && c != 'D' && c != 'C' && c != 'L' && c != 'X'
                    && c != 'V' && c != 'I') {
                invalid_operand = true;
                System.out.println("Invalid Numberal!");
            }
            else
            // else the char is valid, so add it to the ArrayList to be returned
            {
            char numeral = operand.charAt(i);
            numerals.add(numeral);
            }

        }
    } while (invalid_operand);
     return numerals;
}
/* changed the return type of the method to ArrayList<Character> so that when 
   you call the method from another, you retrieve an ArrayList containing 
   just the Characters corresponding to the chars you want */

ArrayList<Character> getOperand(int which) {
    System.out.println("Enter operand " + which);
    boolean invalid_operand;
    ArrayList<Character> numerals = new ArrayList<Character>();

    do {
        invalid_operand = false;

        String operand = keyboard.nextLine();
        int length = operand.length();
        if (operand.length() == 0)
            invalid_operand = true;

        for (int i = 0; i < length; i++) {
            char c = operand.charAt(i);

            if (c != 'M' && c != 'D' && c != 'C' && c != 'L' && c != 'X'
                    && c != 'V' && c != 'I') {
                invalid_operand = true;
                System.out.println("Invalid Numberal!");
            }
            else
            // else the char is valid, so add it to the ArrayList to be returned
            {
            char numeral = operand.charAt(i);
            Character character = new Character(numeral);
            numerals.add(character);
            }

        }
    } while (invalid_operand);
     return numerals;
}