2013-07-19 60 views
0

我正在制作一个猪拉丁语翻译,我真的不知道从哪里去。我有基本的代码,但我需要修改它以便翻译整个句子。如果有人能告诉我如何使用我的字符串[]字我真的很感激它。非常感谢!猪拉丁语翻译(句子)

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"); 
         } 
       } 
      } 
     } 
    } 
} 
} 
+1

为了澄清起见,您能否定义您的猪拉丁文设计​​? – hexafraction

+0

基本上,如果一个单词的前四个字母中的任何一个都是辅音,就用“ay”将它们添加到后面。如果单词以元音开头,则在单词的末尾添加“方式”。 – Gihadi

回答

0

这应该是阵列中的每个单词翻译,只需使用一个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],因为那是您想要使用的变量。

+0

我使用了替代方法,但遇到了一个小问题。我正朝着正确的方向前进,但现在它只能翻译四个字。这是否与我有String answer4 = words [i] .substring94,words [i] .length())这个事实有关?我怎样才能让它循环无限,直到空白? – Gihadi

+0

现在你有一个单词列表,这个循环将一次一个地遍历每一个这样的单词。不确定你在问什么。随意添加代码到你的问题,我会看看@Gihadi – Stephan

+0

@Gihadi如果它正在翻译4个单词,那么这应该是一个非常简单的修复,让我知道什么是错的 – Stephan

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"); 
        } 
       } 
      } 
     } 
    } 
} 
0

您可以使用循环增强在你如果块整个数组翻译:

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

虽然创建这样的翻译是相当不安全的(例如代码注入),除非输入是从可信源放英寸

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

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(); 
}