Pig Latin Translate(Sentence)

时间:2013-07-19 19:10:25

标签: java arrays translate

我正在制作一个Pig Latin翻译器,我真的不知道从哪里开始。我有基本代码,但我需要修改它以便翻译整个句子。如果有人能告诉我如何使用我的String []字,我会非常感激。非常感谢!

import java.io.*;
import java.util.*;
import java.util.Arrays;

public class Main
{
  public static void main (String[] args) 
  {
    System.out.print("Please enter a phrase to translate: ");
    Scanner scan = new Scanner(System.in);
    String str = scan.nextLine();  
    String[] words = str.split("\\s+");
    String answer = "";
    if (str.startsWith("a") || str.startsWith("e") || str.startsWith("i") || str.startsWith("o") || str.startsWith("u"))
    {
      System.out.print(str + "way");
      }
    else
      {
        answer = str.substring(2,str.length());
        String answer2 = str.substring(1,str.length());
        String answer3 = str.substring(3,str.length());
        String answer4 = str.substring(4,str.length());
        String d = str.substring(0,4);
        if (!(d.contains("a") || d.contains("e") || d.contains("i") || d.contains("o") || d.contains("u")))
          {
            System.out.print(answer4 + d + "ay");
            }
        else 
        {
            String c = str.substring(0,3);
            if (!(c.contains("a") || c.contains("e") || c.contains("i") || c.contains("o") || c.contains("u")))
          {
            System.out.print(answer3 + c + "ay");
            }
            else
            {
                String b = str.substring(0,2);
                if (!(b.contains("a") || b.contains("e") || b.contains("i") || b.contains("o") || b.contains("u")))
                {
                    System.out.print(answer + b + "ay");
                    }
                else
                {
                    String a = str.substring(0,1);
                    if (!(a.contains("a") || a.contains("e") || a.contains("i") || a.contains("o") || a.contains("u")))
                    {
                        System.out.print(answer2 + a + "ay");
                        }
                }
            }
        }
    }
}
} 

4 个答案:

答案 0 :(得分:0)

这应该翻译该数组中的每个单词,只需在整个if块

周围使用for循环
for(String word: words) {
   //put your whole if block here
   //word is the variable to translate (i.e. word.startsWith("a"))
}

或者初学者:

for(int i = 0; i < words.length(); i++) {
  //put if statements here
  //words[i] is the variable to translate (i.e. words[i].startsWith("a"))
}

您还需要将循环中的所有str引用更改为word[i],因为这是您要使用的变量。

答案 1 :(得分:0)

你可以有一个遍历每个单词的for循环,并将你的逻辑应用于每个单词:

for(String str : words ) {

    if (str.startsWith("a") || str.startsWith("e") || str.startsWith("i") || str.startsWith("o") || str.startsWith("u"))
    {
        System.out.print(str + "way");
    }
    else
    {
        answer = str.substring(2,str.length());
        String answer2 = str.substring(1,str.length());
        String answer3 = str.substring(3,str.length());
        String answer4 = str.substring(4,str.length());
        String d = str.substring(0,4);
        if (!(d.contains("a") || d.contains("e") || d.contains("i") || d.contains("o") || d.contains("u")))
        {
            System.out.print(answer4 + d + "ay");
        }
        else 
        {
            String c = str.substring(0,3);
            if (!(c.contains("a") || c.contains("e") || c.contains("i") || c.contains("o") || c.contains("u")))
            {
                System.out.print(answer3 + c + "ay");
            }
            else
            {
                String b = str.substring(0,2);
                if (!(b.contains("a") || b.contains("e") || b.contains("i") || b.contains("o") || b.contains("u")))
                {
                    System.out.print(answer + b + "ay");
                }
                else
                {
                    String a = str.substring(0,1);
                    if (!(a.contains("a") || a.contains("e") || a.contains("i") || a.contains("o") || a.contains("u")))
                    {
                        System.out.print(answer2 + a + "ay");
                    }
                }
            }
        }
    }
}

答案 2 :(得分:0)

您可以在 if 块周围使用增强的for循环来翻译整个数组:

for(String word: words) {
  /*
   *if block goes here
   *...
   */ 
}

尽管创建这样的翻译器是相当不安全的(例如代码注入),除非输入是从可靠来源输入的。

如果您熟悉词法分析器/解析器,则应考虑使用ANTLR之类的生成器来创建更安全(更复杂)的语言翻译器。

答案 3 :(得分:0)

#include stdio.h>
#include conio.h>
#include stdlib.h>
#include string.h>

void main()
{
       char *  sUserInput ;
       char *  check      ;
       char *  firstletter;

       char    ch    = NULL;
       int     i          = 0   ;
       int     j          = 0   ;
       int     k          = 0   ;
       int     spacecount = 0   ;
       int     count      = 0   ;
       int     count1     = 0   ;
       int     var        = 0   ;
       int     gold       = 1   ;

       sUserInput  = (char *) malloc(sizeof(char));
       check       = (char *) malloc(sizeof(char));
       firstletter = (char *) malloc(4*sizeof(char));

       printf("Enter the sentence \n");

       while(ch != '\n')
       {
             scanf("%c",&ch);
             sUserInput[i]=ch;
             sUserInput = (char *) realloc(sUserInput,((i+2))*sizeof(char));
             i++;
             if( ch == ' ')
             {
                 spacecount++;
             }
       }

       while(sUserInput[j] != '\n')
       {
             count1++;
             if(gold==1)
             {
                    firstletter[var]=sUserInput[j];
                    firstletter[var+1]='a';
                    firstletter[var+2]='y';
                    gold=0;
                    j++;
             }
             if(sUserInput[j] !=' ')
             {
                    check[k] = sUserInput[j];
                    check = (char *) realloc(check,((k+2))*sizeof(char));
                    printf("%c",check[k]);
                    k++;
             }
             if(sUserInput[j] ==' ' || (count1 == (i-(spacecount+2))))
             {
                    for(count=0;count<3;count++)
                    {
                           printf("%c",firstletter[count]);
                    }
                    var=0;
                    gold=1;
                    printf(" ");
             }
             j++;
       }

       free(sUserInput);
       free(check);
       free(firstletter);
       getch();
}