如何使用java将英语翻译成PigLatin(使用布尔和字符串) [英] How to translate English into PigLatin with java (using boolean and string)

查看:213
本文介绍了如何使用java将英语翻译成PigLatin(使用布尔和字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个程序,检查用户被约束的句子,并将其转换为Pig Latin。我试图让程序检查,看看第一个字母是否为元音,并返回使用布尔表达式。接下来,我试图让程序切断第一个字母的单词,并将其添加到单词的结尾。最后,如果它是元音,则应该添加方式,如果不是元音,则 ay 任何帮助或建议将非常感激。

  public class PigLatin {
public static void main(String [] argv){
if .length> 0){
for(int i = 0; i char firstLetter = aStringVariable.charAt(0);
}
}
public static boolean isVowel(char c){
char [] vowels = new char [] {'a','e','i' o','u','y'};
for(int i = 0; i if(Character.toString(vowels [i])。equalsIgnoreCase(Character.toString(c))){
return true;
}
}
return false;
}
public static String makePigLatin(boolean vowel,String word)
{
String everythingButTheFirstLetter = word.substring(1);

String n = everythingButTheFirstLetter + firstLetter;

if(true)
{
System.out.println(n +way);
}
if(false)
{
System.out.println(n +ay);
}
}
}


解决方案>

尝试这样 -

  public static class PigLatin {
public static boolean isVowel(char c) {
switch(Character.toLowerCase(c)){
case'a':
case'e':
case'i':
case' :
case'u':
case'y':
return true;
}
return false;
}

public static String makePigLatin(String word){
char firstLetter = word.charAt(0);
String everythingElse = word.substring(1);

String n = everythingElse + firstLetter;

if(isVowel(firstLetter)){
return n +way;
}
return n +ay;
}
}

public static void main(String args []){
String [] words = {fair,away,test };
for(String word:words){
String s = PigLatin.makePigLatin(word);
System.out.println(word +is+ s);
}
}

输出为

  fair is airfay 
away is wayaway
test is esttay


I'm trying to write a program that checks a user supploed sentence and converts it to Pig Latin. I'm trying to have the program check to see if the first letter is a vowel or not and return that using a boolean expression. Next I'm trying to get the program to cut off the first letter from the word and add it to the end of the word. Finally its supposed to add way if it is a vowel and ay if it not a vowel. Any help or advice will be greatly appreciated.

public class PigLatin     {
  public static void main(String[] argv) {
    if (argv.length > 0) {
      for (int i = 0; i < argv.length; i++) {
        char firstLetter = aStringVariable.charAt(0);
    }
  }
      public static boolean isVowel(char c) {
      char[] vowels = new char[] {'a', 'e', 'i', 'o', 'u', 'y'};
        for(int i = 0; i < vowels.length; i++) {
            if(Character.toString(vowels[i]).equalsIgnoreCase(Character.toString(c)))  {
                return true;
            }
        }
        return false;
    }
    public static String makePigLatin(boolean vowel, String word)
    {
      String everythingButTheFirstLetter = word.substring(1); 

      String n = everythingButTheFirstLetter + firstLetter;

    if (true)
    {
      System.out.println(n + "way");
    }
    if (false)
    {
      System.out.println(n + "ay");
    }
    }
}

解决方案

Try something like this -

public static class PigLatin {
  public static boolean isVowel(char c) {
    switch (Character.toLowerCase(c)) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
    case 'y':
      return true;
    }
    return false;
  }

  public static String makePigLatin(String word) {
    char firstLetter = word.charAt(0);
    String everythingElse = word.substring(1);

    String n = everythingElse + firstLetter;

    if (isVowel(firstLetter)) {
      return n + "way";
    }
    return n + "ay";
  }
}

public static void main(String args[]) {
  String[] words = { "fair", "away", "test" };
  for (String word : words) {
    String s = PigLatin.makePigLatin(word);
    System.out.println(word + " is " + s);
  }
}

Output is

fair is airfay
away is wayaway
test is esttay

这篇关于如何使用java将英语翻译成PigLatin(使用布尔和字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆