2011-01-28 63 views
0

我似乎無法得到這個工作,這是我的第一個Java類,所以任何的幫助深表感謝: 繼承人是我迄今爲止:strEnglishPhrase已經在main(java.lang.String [])中定義了嗎?

import java.io.Console; 
import java.util.Scanner; 

public class Pig extends Object{ 

    public static void main(String[] args){ 
     Console console = System.console(); 
     String strEnglishPhrase; 
     char choice; 
     do{ 
      System.out.println("Welcome to the Pig Latin Translator!!"); 
      strEnglishPhrase = 
       console.readLine("Enter Phrase for Translation : "); 
      Scanner scanner = new Scanner(System.in); 

      String strEnglishPhrase = scanner.nextLine(); 

      if(strEnglishPhrase != null && !strEnglishPhrase.equals("")){ 
       System.out.println("Phrase in Pig Latin : \n" 
        + convertEnglishToPigLatin(strEnglishPhrase)); 
      } else{ 
       System.out.println("Whoops, Invalid Entry."); 
      } 
      choice = 
       console 
        .readLine("Do you want to continue? y" + '/' + "n?") 
        .charAt(0); 
     } while((choice != 'n') && (choice != 'N')); 
    } 

    public static String convertEnglishToPigLatin(String strEnglishPhrase){ 
     String strVowels = "aeiou"; 
     String[] strTokens = strEnglishPhrase.split("[ ]"); 
     StringBuffer sbPigLatinStuff = new StringBuffer(); 

     for(int i = 0; i < strTokens.length; i++){ 
      if(strVowels.indexOf(strTokens[i].charAt(0)) >= 0){ 
       sbPigLatinStuff.append(strTokens[i] + "way "); 
      } else if((strTokens[i].indexOf("a") < 0) 
       && (strTokens[i].indexOf("e") < 0) 
       && (strTokens[i].indexOf("i") < 0) 
       && (strTokens[i].indexOf("o") < 0) 
       && (strTokens[i].indexOf("u") < 0)){ 
       sbPigLatinStuff.append(strTokens[i] + "ay "); 
      } else{ 
       for(int j = 1; j < strTokens[i].length(); j++){ 
        if(strVowels.indexOf(strTokens[i].charAt(j)) >= 0){ 
         sbPigLatinStuff.append(strTokens[i].substring(j) 
          + strTokens[i].substring(0, j) + "ay "); 
         break; 
        } 
       } 
      } 
     } 

     return sbPigLatinStuff.toString(); 
    } 
} 
+0

使用IDE(如Eclipse)。它會立即告訴你什麼是錯的。 – 2011-01-28 08:21:00

回答

1

我的Java不是才華,但你似乎有在您的main方法開始時聲明strEnglishPhrase,然後再次您的while循環。

public static void main(String[] args)  
{ 
Console console = System.console(); 
String strEnglishPhrase; <<-- declared here 
char choice; 
do 
{ 
System.out.println("Welcome to the Pig Latin Translator!!"); 
strEnglishPhrase = console.readLine("Enter Phrase for Translation : "); 
    Scanner scanner = new Scanner(System.in); 

    String strEnglishPhrase = scanner.nextLine(); <<-- And again here 
1

我覺得現在的問題是,在你的代碼的頂部,你有

Console console = System.console(); 
String strEnglishPhrase; 
char choice; 

它定義了一個名爲StringstrEnglishPhrase。然而,在循環體中,您也可以編寫

String strEnglishPhrase = scanner.nextLine(); 

這將嘗試定義一個新的變量也被稱爲strEnglishPhrase,它與先前的定義有衝突。

爲了解決這個問題,無論是從外循環刪除聲明,或改變上面的代碼來讀取

strEnglishPhrase = scanner.nextLine(); 

其是分配,而不是一個聲明。

2

您在同一範圍內有一個字符串變量strEnglishPhrase的雙重聲明。

public class Pig extends Object 
{ 
    public static void main(String[] args)  
    { 
     Console console = System.console(); 

     // First declaration of strEnglishPhrase! 
     String strEnglishPhrase; 

     char choice; 

     do { 
      System.out.println("Welcome to the Pig Latin Translator!!"); 
      strEnglishPhrase = console.readLine("Enter Phrase for Translation : "); 
      Scanner scanner = new Scanner(System.in); 

      // Second and duplicate declaration of strEnglishPhrase! 

      String strEnglishPhrase = scanner.nextLine(); 

      ... 
     } 
    ... 
    } 
} 

你可以在你的do循環去除類型聲明strEnglishPhrase解決的問題:

// Replace this line 
String strEnglishPhrase = scanner.nextLine(); 

// by this line: 
strEnglishPhrase = scanner.nextLine(); 
0

您宣佈strEnglishPhrase首次在這裏:

Console console = System.console(); 
String strEnglishPhrase; 
char choice; 

然後在此處再次定義它:

String strEnglishPhrase = scanner.nextLine(); 

現在就刪除'String'關鍵字。

strEnglishPhrase = scanner.nextLine(); 
相關問題