You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Java代码实现多单词(句子)的Pig Latin翻译?

问题:Pig Latin整句翻译仅输出第一个单词的解决方法

问题背景

我编写了一段Java代码可将单个单词转换为Pig Latin形式,但修改为翻译整句内容时,代码仅能输出第一个单词,其余内容丢失。

原单个单词处理代码

import java.util.Scanner;

class Main {
  static Scanner myObj = new Scanner(System.in);

  static boolean isVowel(char c) {
    return (c == 'A' || c == 'a' || c == 'E' || c == 'e' || c == 'I' || c == 'i' || c == 'O' || c == 'o' || c == 'U'
        || c == 'u');
  }

  static String pigLatin(String oldWord) {
    System.out.println("\r\n");
    System.out.println("What word should I translate?");
    oldWord = myObj.nextLine();
    System.out.println("");
    try {
      Thread.sleep(800);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print("\r\n");
    System.out.print("\r\n");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    int len = oldWord.length();
    int index = -1;
    for (int i = 0; i < len; i++) {
      if (isVowel(oldWord.charAt(i))) {
        index = i;
        break;
      }
    }

    if (index == -1)
      return "-1";

    return oldWord.substring(index) + oldWord.substring(0, index) + "ay";
  }

  public static void main(String[] args) {
    String newWord = pigLatin("graphic");
    if (newWord == "-1")
      System.out.print("No vowels found. Pig Latin not possible");
    else {
      System.out.print("Your word in Pig Latin is: \033[1m" + newWord + "\033[0m");
    }
  }
}

整句处理的问题代码

import java.util.Scanner;

class Main {
  static Scanner myObj = new Scanner(System.in);

  static boolean isVowel(char c) {
    return (c == 'A' || c == 'a' || c == 'E' || c == 'e' || c == 'I' || c == 'i' || c == 'O' || c == 'o' || c == 'U'
        || c == 'u');
  }

  static String pigLatin(String sentance) {
    System.out.println("\r\n");
    System.out.println("What sentance should I translate?");
    String oldWord = myObj.nextLine();
    System.out.println("");
    try {
      Thread.sleep(800);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print("\r\n");
    System.out.print("\r\n");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    
    String[] words = oldWord.split(" ");
    for (String word : words) {
      int len = word.length();
      int index = -1;
      for (int i = 0; i < len; i++) {
        if (isVowel(word.charAt(i))) {
          index = i;
          break;
        }
      }
      if (index == -1)
        return "-1";
      return word.substring(index) + word.substring(0, index) + "ay";
    }
    return words.toString();
  }

  public static void main(String[] args) {
    String newWord = pigLatin("graphic");
    System.out.print("Your sentance in Pig Latin is: \033[1m" + newWord + "\033[0m");
  }
}

问题原因

  1. 提前返回导致中断循环:处理第一个单词时直接用return返回结果,方法提前结束,后续单词未被处理。
  2. 数组toString错误:words.toString()返回的是数组内存地址,不是拼接后的字符串。
  3. 字符串比较错误:用==比较字符串内容,这在Java中是错误的,应该用equals()。

修正方案

修正后的代码

import java.util.Scanner;

class Main {
  static Scanner myObj = new Scanner(System.in);

  static boolean isVowel(char c) {
    return (c == 'A' || c == 'a' || c == 'E' || c == 'e' || c == 'I' || c == 'i' || c == 'O' || c == 'o' || c == 'U'
        || c == 'u');
  }

  // 单独提取单词转换逻辑,提升代码可读性
  static String translateWord(String word) {
    int len = word.length();
    int index = -1;
    for (int i = 0; i < len; i++) {
      if (isVowel(word.charAt(i))) {
        index = i;
        break;
      }
    }
    return index == -1 ? null : word.substring(index) + word.substring(0, index) + "ay";
  }

  static String pigLatin(String dummy) {
    System.out.println("\r\n");
    System.out.println("What sentence should I translate?");
    String sentence = myObj.nextLine();
    System.out.println("");
    try {
      Thread.sleep(800);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print(".");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    System.out.print("\r\n");
    System.out.print("\r\n");
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      System.out.println(e);
    }
    
    String[] words = sentence.split(" ");
    StringBuilder result = new StringBuilder();
    
    for (String word : words) {
      String translated = translateWord(word);
      if (translated == null) {
        return "-1"; // 遇到无元音单词,返回错误标识
      }
      if (result.length() > 0) {
        result.append(" "); // 单词间添加空格分隔
      }
      result.append(translated);
    }
    
    return result.toString();
  }

  public static void main(String[] args) {
    String newSentence = pigLatin("graphic");
    if (newSentence.equals("-1")) {
      System.out.print("No vowels found in one of the words. Pig Latin not possible");
    } else {
      System.out.print("Your sentence in Pig Latin is: \033[1m" + newSentence + "\033[0m");
    }
  }
}

关键修改点

  • 使用StringBuilder逐个拼接转换后的单词,避免提前返回中断循环。
  • 单独提取translateWord方法,分离单词转换逻辑,提升代码可维护性。
  • 用equals()替代==进行字符串内容比较。
  • 修正拼写错误:sentance改为sentence。

内容的提问来源于stack exchange,提问作者RETERON

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 04:05:54