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

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

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

以下是翻译好的内容:

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

枚举类:

  1. public enum Enum {
  2. FORWARD('F'),
  3. RIGHT('R'),
  4. LEFT('L');
  5. private Enum(char letter) {
  6. this.letter = letter;
  7. }
  8. private char letter;
  9. public char getLetter() {
  10. return letter;
  11. }
  12. }

方法:

  1. public static Enum[] method(char[] input) throws InstructionException {
  2. Enum[] output = new Enum[input.length];
  3. for (int i = 0; i < input.length; i++) {
  4. boolean found = false;
  5. for (Enum e : Enum.values()) {
  6. if (e.getLetter() == input[i]) {
  7. output[i] = e;
  8. found = true;
  9. break;
  10. }
  11. }
  12. if (!found) {
  13. throw new InstructionException();
  14. }
  15. }
  16. return output;
  17. }

主函数:

  1. public static void main(String[] args) {
  2. char[] input = {'F', 'F', 'R'};
  3. try {
  4. Enum[] output = method(input);
  5. for (Enum e : output) {
  6. System.out.println(e);
  7. }
  8. } catch (InstructionException e) {
  9. System.out.println("ERROR !");
  10. }
  11. }
英文:

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

Enum Class :

  1. public enum Enum {
  2. FORWARD(&#39;F&#39;),
  3. RIGHT(&#39;R&#39;),
  4. LEFT(&#39;L&#39;);
  5. private Enum(char letter) {
  6. this.letter = letter;
  7. }
  8. private char letter;
  9. public char getLetter() {
  10. return letter;
  11. }
  12. }

Method :

  1. public static Enum[] method(char[] input) throws InstructionException {
  2. Enum[] output = new Enum[input.length];
  3. for (int i = 0; i &lt; input.length; i++) {
  4. boolean found = false;
  5. for (Enum e : Enum.values()) {
  6. if (e.getLetter() == input[i]) {
  7. output[i] = e;
  8. found = true;
  9. break;
  10. }
  11. }
  12. if (!found) {
  13. throw new InstructionException();
  14. }
  15. }
  16. return output;
  17. }

Main :

  1. public static void main(String[] args) {
  2. char[] input = {&#39;F&#39;, &#39;F&#39;, &#39;R&#39;};
  3. try {
  4. Enum[] output = method(input);
  5. for (Enum e : output) {
  6. System.out.println(e);
  7. }
  8. } catch (InstructionException e) {
  9. System.out.println(&quot;ERROR !&quot;);
  10. }
  11. }

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:

确定