strEnglishPhrase已在main(java.lang.String [])中定义?

时间:2011-01-28 08:08:26

标签: java string

我似乎无法让它工作,这是我的第一个java类,所以任何帮助都非常感谢: 继续我到目前为止所拥有的:

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

4 个答案:

答案 0 :(得分: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();  

答案 1 :(得分:1)

我的Java并不出色,但您似乎已在strEnglishPhrase方法的开头宣布main,然后再次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

答案 2 :(得分:1)

我认为问题在于代码顶部有

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

其中定义了名为String的{​​{1}}。但是,在循环体中,您还要编写

strEnglishPhrase

这会尝试定义一个名为String strEnglishPhrase = scanner.nextLine(); 的新变量,该变量与之前的定义冲突。

要解决此问题,请从循环外部删除声明,或将上述代码更改为

strEnglishPhrase

这是一项任务而非宣言。

答案 3 :(得分:0)

你第一次在这里声明了strEnglishPhrase:

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

然后在此时再次定义它:

String strEnglishPhrase = scanner.nextLine();

此时只需删除'String'关键字。

strEnglishPhrase = scanner.nextLine();