是否可以保存从迭代字符串中获取的单个char值?

时间:2015-02-20 18:34:31

标签: java

Java Student在这里研究罗马计算器问题。我试图取一个给定的罗马数字串传递它通过for循环并将每个数字保存为char,以便我可以在下面的方法中将其转换为Integer值。有没有办法将每个字符的值保存到变量或可能的数组中,并在我的下一个方法中调用它来为其赋值?以下是我到目前为止的情况:

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;
}

2 个答案:

答案 0 :(得分:2)

借用其他答案并对您的方法做出一些进一步的修改,我相信您可以按照我在下面发布的任何一种方式执行您所询问的内容(存储用其他方法输入的字符) 。第一个返回与有效字符对应的整数列表。第二个将此列表作为字符返回。

但请注意,我怀疑你是否应该解决这个问题。看来你可能已经获得了一个模板,并填补了空白。这是真的?如果我是正确的,方法(int)的返回类型是你给出的模板的一部分,你不应该改变它。在我看来,这个方法应该采用一个操作数(例如CLII,罗马数字代表152)并返回一个int(在这种情况下为152)。如果我对此有误,那么我需要进一步澄清,以便知道这种方法实际上应该做什么。

名称&#39; getOperand&#39;似乎只是表示读取用户输入并将其存储以备将来使用,但为什么返回类型为int?如果预期输入是一系列字符,则int的返回类型会告诉我该方法将输入的罗马数字转换为整数(整数而不是整数。我在这里使用单词整数来指代什么正在进行:将Roman数字转换为整数(例如整数152)而不是Integer(您可能选择用来帮助实现此目的的Java类))。

也许你可以澄清一下这个方法应该做什么,从一开始就给你一部分方法,如果是的话,确切地给你什么部分给你以及你自己添加了什么。

现在我已经说过,这是我上面提到的两种方法。

方法1(整数):

 /* 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;
}

方法2(作为字符):

/* 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;
}

实际上,一种更简洁的方法可能只是将返回类型更改为String,实例化一个新的String而不是一个ArrayList(将其初始值设置为&#39;&#39;)并附加每个有效的char它。 (已经说过,我会重申,如果作为模板提供给你,你不应该对返回类型进行此更改或任何其他更改)。这应该是直截了当的,但对于你来说,找到如何为自己找到这些练习的答案是一个有用的练习。我会google&#39; String追加&#39;作为一个起点,看看出现的链接,看看是否有什么可以帮助。您可能也想查看StringBuilder类,看看它是否是最好使用的类。您可能会发现String类具有您需要的所有内容,而无需使用StringBuilder。

如果您不熟悉ArrayList,Integer和Character类(或任何其他类)的操作,我建议你谷歌(例如)&#39; ArrayList Java& #39;并查看第一个链接中的文档,您可以在其中找到方法列表和其他有用信息。你可能不会立即理解所有内容,但对于任何学会熟悉这一点的人都有帮助(我以前的讲师这么说)。我相信它被称为Java doc&#39;。

答案 1 :(得分:0)

List<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