我需要帮助在Java中进行数组比较,并进行错误检查。

huangapple go评论74阅读模式
英文:

I need help comparing arrays with error checking in Java

问题

我查看了这里以前的一些类似问题,但似乎没有什么我尝试过可以修复我的程序。我正在进行一个用于学习Java的项目,我在这个特定部分遇到了问题。

我需要比较两个字符数组,并进行错误检查,然后打印与第一个数组中的字符对应的枚举常量数组。我尝试过使用嵌套for循环、增强for循环,还尝试将其设置为哈希集。我需要帮助。

第一个字符数组来自用户输入的字符串:例如,用户输入"FFRL",它被放入一个字符数组['F', 'F', 'R', 'L']。第二个字符数组来自分配的枚举值FORWARD('F'),RIGHT('R'),LEFT('L'),它是['F', 'R', 'L']。如果给出无效输入,如B,就抛出一个错误。指令建议使用continue和/或break。这是预期的测试结果:

  • 输入:FFRL
  • 输出:{FORWARD FORWARD RIGHT LEFT}
  • 输入:FFB
  • 输出:INVALID INSTRUCTION EXCEPTION

该程序已经有一些预定义的方法,但我还没有在程序的循环部分中得到太多使用。如果我没有清楚地呈现这个问题,请告诉我是否更愿意看到代码或实际指令的一部分。

英文:

I looked at a bunch of previously similar issues on here and nothing I tried seems to fix my program. I'm working on a project that is used for learning Java and I'm hung up on this one section.

I need to compare two char arrays, with error checking, and print an array of enum constants that corresponds to the chars in the first array. I've tried doing a nested for loop, enhanced for loop, tried setting it up as hashsets. I need help.

The first array of chars comes from a user input string: ex. user inputs "FFRL", it gets put into a char array ['F', 'F' ,'R' ,'L']. The second array of chars is from assigned enum values FORWARD('F'), RIGHT('R'), LEFT('L') ['F', 'R', 'L']. Throw an error if given invalid input, such as B. Instructions suggest continue and/or break. Here's the expected testing outcome:

  • input: FFRL
  • output: {FORWARD FORWARD RIGHT LEFT}
  • input: FFB
  • output: INVALID INSTRUCTION EXCEPTION

The program does have some predefined methods but I haven't gotten much use out of them yet for the loop part of the program. Let me know if you rather see code or part of the actual instructions if i didn't present this clearly.

答案1

得分: 0

以下是翻译好的内容:

如果我理解正确,这就是你所寻找的:

枚举类:

public enum Enum {

   FORWARD('F'),
   RIGHT('R'),
   LEFT('L');

   private Enum(char letter) {
      this.letter = letter;
   }

   private char letter;

   public char getLetter() {
     return letter;
   }
}

方法:

public static Enum[] method(char[] input) throws InstructionException {
    Enum[] output = new Enum[input.length];
    for (int i = 0; i < input.length; i++) {
        boolean found = false;
        for (Enum e : Enum.values()) {
            if (e.getLetter() == input[i]) {
                output[i] = e;
                found = true;
                break;
            }
        }
        if (!found) {
            throw new InstructionException();
        }
    }
    return output;
}

主函数:

public static void main(String[] args) {
    char[] input = {'F', 'F', 'R'};
    try {
        Enum[] output = method(input);
        for (Enum e : output) {
            System.out.println(e);
        }
    } catch (InstructionException e) {
        System.out.println("ERROR !");
    }
}
英文:

If i understood correctly, this is what you're looking for :

Enum Class :

public enum Enum {

   FORWARD(&#39;F&#39;),
   RIGHT(&#39;R&#39;),
   LEFT(&#39;L&#39;);

   private Enum(char letter) {
      this.letter = letter;
   }

   private char letter;

   public char getLetter() {
     return letter;
   }
}

Method :

public static Enum[] method(char[] input) throws InstructionException {
    Enum[] output = new Enum[input.length];
    for (int i = 0; i &lt; input.length; i++) {
        boolean found = false;
        for (Enum e : Enum.values()) {
            if (e.getLetter() == input[i]) {
                output[i] = e;
                found = true;
                break;
            }
        }
        if (!found) {
            throw new InstructionException();
        }
    }
    return output;
}

Main :

public static void main(String[] args) {
    char[] input = {&#39;F&#39;, &#39;F&#39;, &#39;R&#39;};
    try {
        Enum[] output = method(input);
        for (Enum e : output) {
            System.out.println(e);
        }
    } catch (InstructionException e) {
        System.out.println(&quot;ERROR !&quot;);
    }
}

huangapple
  • 本文由 发表于 2020年10月26日 00:18:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64525969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定