【问题标题】:Yet another Pig Latin translator又一个猪拉丁语翻译
【发布时间】:2016-04-21 07:02:39
【问题描述】:

所以我正在尝试使用 JOptionPane 编写一个程序来将一个句子翻译成猪拉丁语我已经大致弄清楚了代码,但是当它返回句子的猪拉丁语版本时我似乎可以得到输出,所以任何帮助都会厉害了

 package piglatin;


 import javax.swing.JOptionPane;


 public class Piglatin {


public static void main(String[] args) 
{

  // String to hold input.
  String input;

  // Get a string to convert.
    input = JOptionPane.showInputDialog("Enter a string and I will convert it to Pig latin.");

  // Convert it to uppercase, for consistency.
  input = input.toUpperCase();



  // Display the Pig Latin translation.
    JOptionPane.showMessageDialog(null,"Your Phrase is: "+ pigLatin);
}

  public class PigLatinator
  {
 private String original; // Original string
 private String pigLatin; // Pig Latin version


 public PigLatinator(String input)
    {
    // Variable to hold each word
    String word;

    // Save the input string.
     original = input;

    // Initialize pigLatin to an empty string.
    pigLatin = "";

    // Trim all leading and trailing whitespaces.
    StringBuilder sb = new StringBuilder(input.trim());

    while (sb.length() > 0)
    {
      // Remove the first word from sb and assign it to word.
         word = popWord(sb);

      // Convert the word to Pig Latin and add it to the
      // Pig Latin sentence.
         pigLatin = pigLatin + toPigLatin(word) + " ";
      } 
   }

    private String popWord(StringBuilder sb)
    {
    // Locate the first space, or the end of the string.
    int index = 0;
    while (index < sb.length() && sb.charAt(index) != ' ')
    {
     index++;
    }

  // Get the word from the beginning of sb.
  String word = sb.substring(0, index);

  // Delete the word from sb.
  sb.delete(0, index+1);

  // Return the extracted word.
  return word;
  }

    private String toPigLatin(String word)
  {
  // Create a StringBuilder.
  StringBuilder sb = new StringBuilder(word);

  // Get the first letter of the word.
  char first = sb.charAt(0);

  // Append the letter to the end of the word.
  sb.append(first);

  // Append "AY" to the word.
  sb.append("AY");

  // Delete the first letter.
  sb.deleteCharAt(0);

  // Return the word.
  return sb.toString();
  }

  /**
  getPigLatin method
  @return The Pig Latin version of the string.
   */
   public String getPigLatin()
  {
  return pigLatin;

  }

  /**
  getOriginal method
  @return The original string.
  */
  public String getOriginal()
  {
  return original;
  }
}  
}

【问题讨论】:

    标签: java swing joptionpane


    【解决方案1】:

    该问题可能的具体问题是 PigLatinator 类在代码中没有实例化。假设它应该是:

    input = JOptionPane.showInputDialog(...);
    
    // need to actually use the class
    PigLatinator pigLatin = new PigLatinator(input);
    
    // display the results of the PigLatinator's efforts
    JOptionPane.showMessageDialog(null,"Your Phrase is: "+ pigLatin.getPigLatin());
    

    但是,OP 代码做的工作太多。

    • 使用 String.split 而不是 popWord 方法拆分输入
    • 它似乎没有考虑以元音开头的单词
    • 追加和删除比仅仅追加更多的工作

    与 OP 不在同一类中,但可以根据需要重新插入。这段代码注释掉了输入只是为了更好地调试,但很容易恢复。

     public static void main(String[] args)
    {
        // String to hold input.
        String input;
    
        // Get a string to convert.
        // input =
        // JOptionPane.showInputDialog("Enter a string and I will convert it to Pig latin.");
    
        input = "Now is the time for all good people to acquiesce to sleep";
    
        // debug output
        System.out.println(input);
    
        // split into worlds
        String[] words = input.split("[\\s]+");
    
        // the output
        StringBuilder output = new StringBuilder();
    
        // process all of the words into pig latin
        for (String word : words) {
            if (output.length() > 0) {
                output.append(" ");
            }
            output.append(toPigLatin(word));
        }
    
        // show the result
        // JOptionPane.showMessageDialog(null, "Converted is " + output.toString());
    
        System.out.println(output);
    }
    
    
    
    static String toPigLatin(final String word)
    {
        Pattern pat = Pattern.compile("^[aeiouAEIOU].*$");
        final String ay = "ay";
        final String vowel_ay = "yay";
    
        StringBuilder sb = new StringBuilder();
    
        // if the word begins with a vowel, then it is the word + "yay";
        Matcher vowel = pat.matcher(word);
        if (vowel.matches()) {
            sb.append(word);
            sb.append(vowel_ay);
        }
        else if (word.length() > 1) {
            // otherwise, take the first letter, move to the end, and add "ay"
            sb.append(word.substring(1));
            sb.append(word.substring(0, 1));
            sb.append(ay);
        }
        else {
            // just append "ay"
            sb.append(word);
            sb.append(ay);
        }
    
        return sb.toString();
    }
    

    示例输出:

    现在是所有好人都默许睡觉的时候了
    owNay isyay hetay imetay orfay allyay oodgay eoplepay otay acquiesceyay otay leepsay

    【讨论】:

    • 您好,感谢您的帮助!它真的帮助我看看我到底做错了什么。这段代码似乎要优雅得多,而且不是到处都是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多