2013-07-24 3 views
-5

나는이 돼지 라틴어 번역기에서 일해 왔으며 나는이 두 개의 (동일한) while 루프가 예상대로 수행되지 않는다는 사실을 제외하고는 거의 완료되었습니다. "My name is"와 같이 번역 할 구를 넣으려고하면 "yMay amenay isway."라고 나와야합니다. 문제는 내가 알지 못하는 이유로 표시된 루프가 무한히 수행되고 있다는 것입니다. 그렇지 않으면이 코드가 제대로 작동하는지 테스트했습니다. 어떻게 작동시키는 지 모르겠습니다. 이견있는 사람? 고마워요!돼지 라틴어 번역기 무한 루프

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

public class PigLatin 
{ 
    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+"); 
    int period = words.length; 
    int spaces = (period - 1); 
    String[] word = Arrays.copyOfRange(words,0,spaces); 
    for (int i = 0; i < word.length; i++) 
    { 
     String a = word[i].substring(0,1); 
     int b = a.length(); 
     int c = word[i].length(); 
     while (b <= 4) //start of thought problem 
     { 
      if (!(a.contains("a") || a.contains("e") || a.contains("i") || a.contains("o") || a.contains("u"))) 
      { 
       a = word[i].substring(0,b); 
       b = b + 1; 
       } 
      } // end of thought problem 
     if (word[i].startsWith("a") || word[i].startsWith("e") || word[i].startsWith("i") || word[i].startsWith("o") || word[i].startsWith("u")) 
     { 
      System.out.print(word[i] + "way"); 
      } 
     else if (!(a.contains("a") || a.contains("e") || a.contains("i") || a.contains("o") || a.contains("u"))) 
     { 
      String answer = word[i].substring(b,c); 
      System.out.print(answer + a + "ay"); 
      } 
     System.out.print(" "); 
     } 
    String end = ""; 
    for (String endArray: Arrays.copyOfRange(words,spaces,period)) 
    { 
     end = end + endArray; 
     } 
    String z = end.substring(0,1); 
    int x = z.length(); 
    int y = end.length(); 
    while (x <= 4) //start of thought problem 
    { 
     if (!(z.contains("a") || z.contains("e") || z.contains("i") || z.contains("o") || z.contains("u"))) 
     { 
      z = end.substring(0,x); 
      x = x + 1; 
      } 
     } //end of thought problem 
    if (end.startsWith("a") || end.startsWith("e") || end.startsWith("i") || end.startsWith("o") || end.startsWith("u")) 
    { 
     System.out.print(end + "way"); 
     } 
    else if (!(z.contains("a") || z.contains("e") || z.contains("i") || z.contains("o") || z.contains("u"))) 
    { 
     String answer = end.substring(x,y); 
     System.out.print(answer + z + "ay"); 
     } 
    System.out.print("."); 
    } 
} 
+11

무엇이 잘못 되었나요? 예상되는 것과 현재 출력은 무엇입니까? –

+0

예상되는 출력은 Pig Latin으로 변환됩니다. 이 루프에서 원하는 것은 각 단어의 자음 또는 자음 그룹에 && z를 할당하는 것입니다. 현재 출력은 아무 것도 아니며 끝없이 실행됩니다. – Gihadi

+0

LOL, @ AdamSiemion을 읽을 수 있다고 생각합니다;). 그는 당신에게 샘플 입력 (예 :'my name is adam')과 출력으로 기대하는 것을 제공 할 것을 요구합니다 (예 :'y-may ame-nay is-way'). – davidfmatheson

답변

0

그것은 당신이 볼 수 있듯이 당신이 잠시 나가 결코 if 문 점에서 결코 경우 같은

while (b <= 4) //start of thought problem 
{ 
    System.out.println("Watch this never end"); //Add this print line 
    if (!(a.contains("a") || a.contains("e") || a.contains("i") || a.contains("o") || a.contains("u"))) 
    { 
     a = word[i].substring(0,b); 
     b = b + 1; 
    } 
} 

while (x <= 4) //start of thought problem 
{ 
    if (!(z.contains("a") || z.contains("e") || z.contains("i") || z.contains("o") || z.contains("u"))) 
    { 
     z = end.substring(0,x); 
     x = x + 1; 
    } 
} 

간다 코드를 포맷하면 도움이 고리. for 루프를 대신 사용해보십시오.

for(int b = a.length(); b <=4; b++) 
+0

내가 가진 것과 다른 점이 무엇인가? – Gihadi

+0

절대적으로 아무것도 ...하지만 if 문 안에 결코 들어 가지 않는다면 코드에서 b 또는 x 값이 절대 증가하지 않는다는 사실을 볼 수 있어야합니다. – orangegoat

+0

잘 무한 루프를 어떻게 수정하겠습니까? – Gihadi

1

코드가 너무 형식이 바뀌 었습니다. 여기서는 메서드로 추출되고 일부 변수의 이름이 바뀌 었습니다. 나는 아직도 어떤 버그도 고치지 않았고 이름 바꾸기는 확장 될 수 있었다.

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

public class PigLatin 
{ 
    String[] GetWords() 
    { 
    System.out.print("Please enter a phrase to translate: "); 
    Scanner scan = new Scanner(System.in); 
    String str = scan.nextLine(); 
    String[] words = str.split("\\s+"); 
    int period = words.length; 
    int spaces = (period - 1); 
    return Arrays.copyOfRange(words,0,spaces); 
    } 

    bool ContainsVowel(String text) 
    { 
    return text.contains("a") || text.contains("e") || text.contains("i") || text.contains("o") || text .contains("u") 
    } 

    bool StartsWithVowel(String text) 
    { 
    return text.startsWith("a") || text.startsWith("e") || text.startsWith("i") || text.startsWith("o") || text.startsWith("u") 
    } 

    String PigLatin(String text) 
    { 
    String prefix = text.substring(0,1); 
    int b = a.length(); 
    int c = text.length(); 
    while (b <= 4) //start of thought problem 
    { 
     if (!ContainsVowel(prefix)) 
     { 
      prefix= text.substring(0,b); 
      b = b + 1; 
     } 
    } // end of thought problem 
    if (StartsWithVowel(text) 
    { 
     return text + "way"; 
    } 
    else if (!ContainsVowel(prefix)) 
    { 
     String answer = text.substring(b,c); 
     return answer + prefix + "ay"; 
    } 
    return " "; 
    } 

    public static void main (String[] args) 
    { 
    String[] words = GetWords(); 
    for (int i = 0; i < words.length; i++) 
    { 
     String translation = PigLatin(words[i]); 
     System.out.print(translation + " "); 
    } 

    String end = ""; 
    for (String endArray: Arrays.copyOfRange(words,spaces,period)) 
    { 
     end = end + endArray; 
    } 

    String z = end.substring(0,1); 
    int x = z.length(); 
    int y = end.length(); 
    while (x <= 4) //start of thought problem 
    { 
     if (!ContainsVowel(z)) 
     { 
      z = end.substring(0,x); 
      x = x + 1; 
     } 
    } //end of thought problem 

    if (StartsWithVowel(end)) 
    { 
     System.out.print(end + "way"); 
    } 
    else if (!ContainsVowel(z)) 
    { 
     String answer = end.substring(x,y); 
     System.out.print(answer + z + "ay"); 
    } 
    System.out.print("."); 
    } 
} 

이것은 논리에 많은 도움이됩니다. 그것은 훨씬 더 읽기 쉽고 당신이하려고하는 것을 이해하기 시작할 수 있습니다.

개선 할 부분이 많이 있습니다. 가장 먼저 할 일은 if (StartsWithVowel(end))PigLatin(String text) 메서드의 시작 부분으로 이동하는 것입니다. 그렇게하면 여분의 일을하지 않거나 잘못된 가정을 기반으로 루프를 입력하지 않아도됩니다.