2011-01-28 62 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(); 
相关问题