0

你好 Stackoverflow 社区。我目前正在完成我在网上找到的旧 CS 课程学习作业。我需要使用数组将英语单词转换为法语,反之亦然,使用 2 个字符串数组和线性搜索。我设置了线性搜索,但在设置从第二个数组绘制输出的过程时遇到了困难。以下是我到目前为止汇总的内容,但正如我所说,我很难从输入中提取输出。任何指导表示赞赏!

import java.text.*; // general package for formatting
import javax.swing.*; // for GUI
public class Translation
{
   public static void main(String[] args)
   {
      String[] eng = {"hello", "goodbye", "cat", "dog"};
      String[] fre = {"bonjour", "au revoir", "le chat", "le chien"};
      String word;

      word = JOptionPane.showInputDialog("Enter word");
      sequentialSearch(eng, word);  
   }   

   public static int sequentialSearch(new words[], int target)
   {
      int index;
      int element;
      boolean found;
      index = 0;
      element = -1;
      found = false;

      while (!found && index < words.length)
      {
         if (words[index] == target)
         {
            found = true;
            element = index;
         }
         index++
      }
      return element;   
   }     
}       
4

1 回答 1

0

由于单词是一对一的匹配,您只需要fre在与英语单词相同的索引处显示数组中的单词。

换句话说,如果搜索与条目匹配,eng[1]那么您希望在fre[1]

于 2014-10-14T12:14:05.277 回答