2013-07-19 2 views
0

나는 돼지 라틴어 번역가를 만들고 있는데, 나는 여기서 어디로 가야할 지 모르겠다. 나는 기본 코드를 가지고 있지만 전체 문장을 번역하기 위해서는 그것을 수정해야한다. 누구나 내 String [] 단어를 사용하는 방법을 말해 줄 수 있다면 정말 감사하겠습니다. 고마워요! 또한 변경해야합니다돼지 라틴어 번역 (문장)

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

:

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(String word: words) { 
    //put your whole if block here 
    //word is the variable to translate (i.e. word.startsWith("a")) 
} 

경우 전체 주위에 루프를 사용 모든 str은 루프 내부에서 word[i]으로 참조합니다. 사용하려는 변수이기 때문입니다.

+0

대체 방법을 사용했지만 약간의 문제가 발생했습니다. 나는 올바른 방향으로 나아 갔지만 지금은 4 단어까지만 번역 할 것입니다. 이것은 내가 문자열 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(); 
} 
관련 문제