java - 是否可以保存从字符串迭代中获取的各个字符值?

标签 java

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) 是给定模板的一部分,那么您不应该更改它。在我看来,这个方法应该接受一个操作数(例如 CLII,代表 152 的罗马数字)并返回一个 int(在本例中为 152)。如果我对此有误,那么我需要进一步澄清才能知道这个方法实际上应该做什么。

名称“getOperand”似乎仅表示读取用户输入并将其存储以供将来使用,但是为什么返回类型会是 int 呢?如果预期的输入是一系列字符,则返回类型 int 会建议我该方法将输入的罗马数字转换为整数(整数而不是整数。我在这里使用整数一词来指代正在发生的事情on:将罗马数字转换为整数(例如整数 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(将其初始值设置为 '')并将每个有效字符附加到其中。 (话虽如此,我要重申,如果返回类型是作为构建模板提供给您的,则不应对其进行此更改或任何其他更改)。这应该足够简单,但对于您了解如何自己找到此类练习的答案来说,这是一个有用的练习。我会用谷歌搜索“字符串附加”作为起点,然后查看出现的链接,看看是否有任何可以提供帮助的内容。您可能还想查看 StringBuilder 类,看看它是否是最好使用的类。您可能会发现 String 类拥有您需要的一切,而无需使用 StringBuilder。

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

关于java - 是否可以保存从字符串迭代中获取的各个字符值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28635867/

相关文章:

java - 将字符串拆分为子字符串并将其放入 String [] 中

java - 从不同的文件夹注入(inject) bean

java.lang.RuntimeException : There is no web component by the name of application here

java ArrayList 与字节数组 : ArrayList cannot be resolved to a type error

java - 无法在 Android 中导入 com.google.cloud.speech.v1.SpeechGrpc

java - 为什么永远不会到达collect方法中的BiConsumer Combiner代码?

java - 当 sqlloader 完成数据加载时收到通知

java - 使用java的多列顺序

java - 在 Java 中,如何在方法参数中正确使用抽象类?

java - 如何将自定义链表转换为数组?