繁体   English   中英

比较两个数组时遇到问题

[英]Having problems when comparing two arrays

该程序应该将一个单词从美国版本翻译为英国版本。 它仅适用于第一个单词,但不适用于其他单词,因为它改为提供else语句。

我的代码:

import java.util.Scanner;

public class BritishTranslator {

    public static void main(String[]args) {
        Scanner input = new Scanner(System.in);
        String word;

        String [] america = new String[8];
        String [] british = new String[8];

        america[0] = "attic";
        america[1] = "business suit";
        america[2] = "elevator";
        america[3] = "frenc fries";
        america[4] = "ice cream";
        america[5] = "sneakers";
        america[6] = "truck";
        america[7] = "zero";

        british[0] = "loft";
        british[1] = "lounge suit";
        british[2] = "lift";
        british[3] = "chips";
        british[4] = "ice";
        british[5] = "plimsolls";
        british[6] = "lorry";
        british[7] = "nough";

        System.out.println("Please enter an American word: ");
        word = input.nextLine();

        for (int i = 0; i < america.length; i++)

        {
            for(int j = 0; j < british.length; j++)

            {
                if (word.equals(america[i])) 

                {
                    System.out.println(america[i] + " in british is: " + british[j]);
                    System.exit(0);
                }

                else

                {
                    System.out.println("Word not found in the dictionary.");
                    System.exit(0);
                }
            }
        }
    }
}

我需要学习如何调试此代码的帮助。

您只需要遍历America数组,如果您发现单词看一下英国数组中的相同索引。 和其他的循环

    for (int i = 0; i < america.length; i++){
            if (word.equals(america[i])) {
                System.out.println(america[i] + " in british is: " + british[i]);
                System.exit(0);
            }
    }
    System.out.println("Word not found in the dictionary.");
    System.exit(0); // you dont need this as well

}

由于2个数组在同一索引中包含单词翻译,因此您不必遍历第二个表。 只需在第一个表中找到单词的索引,然后使用该索引在第二个表中获取翻译。 还使用foundboolean标志在循环后检查是否未找到该单词:

System.out.println("Please enter an American word: ");
word = input.nextLine();

boolean found = false;
for (int i = 0; i < america.length; i++) {
    found = word.equals(america[i]);
    if (found) {
        System.out.println(america[i] + " in british is: " + british[i]);
        break;
    }
}
if (!found)
    System.out.println("Word not found in the dictionary.");

通过使用break ,一旦找到单词,循环就会停止。

由于您是将美式单词与法语单词配对,因此只需要循环一次数组即可,如果该单词等于您的美式单词,那么您希望在法语数组和美式数组中以相同的索引打印字符串。

for (int i = 0; i < america.length && i < british.length; i++)
{
    if (word.equals(america[i])) 
    {
        System.out.println(america[i] + " in british is: " + british[i]);
        System.exit(0);
    }
}

System.out.println("Word not found in the dictionary.");

我相信您在这里有几件事。

首先,我认为您不希望在遍历循环之后再执行else语句。 正如当前所写的那样,由于您的if / else和System.exit(0)调用的性质,第一个“ if”语句失败后它将终止。

System.out.println("Please enter an American word: ");
word = input.nextLine();

for (int i = 0; i < america.length; i++)

{
    for(int j = 0; j < british.length; j++)

    {
        if (word.equals(america[i])) 

        {
            System.out.println(america[i] + " in british is: " + british[j]);
            System.exit(0);
         } 
    }
}
System.out.println("Word not found in the dictionary.");
System.exit(0);

但是,实际上并不需要第二个循环,因为有了在第一个循环中迭代时发现的索引,因此可以进一步简化答案。

System.out.println("Please enter an American word: ");
word = input.nextLine();

for (int i = 0; i < america.length; i++)

{
        if (word.equals(america[i])) 

        {
            System.out.println(america[i] + " in british is: " + british[i]);
            System.exit(0);
         } 
}
System.out.println("Word not found in the dictionary.");
System.exit(0);

由于这些值是通过索引映射的,因此您无需第二个循环就可以直接访问要翻译的内容,从而使程序更快,更简单!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM