1

好吧,所以我尝试将下面的语句转换为返回语句,finalString但它总是告诉我,即使我确实返回finalString“此语句必须返回字符串类型的变量”。我已经尝试将return finalString每个单独的 if 语句放入 for 语句中,但它不起作用。我将不胜感激任何帮助或建议。[更新代码] 仍然不起作用。finalString 值不会被 if 语句修改,这正是我想要它做的。我认为也许 finalString 值没有通过 if 语句?

[代码]

import java.util.Scanner;
public class pLat//pig latin program
{

    /**
       * Method to test whether a character is a letter or not.
       * @param c The character to test
       * @return True if it's a letter
       */
      private static boolean isLetter(char c) {
        return ( (c >='A' && c <='Z') || (c >='a' && c <='z') );
      }


    ///////////////////////////////////////////
      private static String output(String input)//processes the word using basic rules including the q and u rule
      {

          //the string that will hold the value of the word entered by the user
          char s;//the first character of the string
          char m;
          int l = input.length();//determines the length of the string
          String endString;
          String startString;
          String finalString = ""; //the final output
          String mtr;
          String lowercase;//the entered string all converted to lowercase

          for(int k =0;k<l;k++)//checks all letters in order to see which is a vowel
          {

              s = input.charAt(k);

          if(s == 'q'|| s=='Q' && input.charAt(k+1)=='u')//if the first vowel is a "u" and the letter before it is a "q"
          {


                  endString = input.substring(0,k+2);//makes the endString also include u
                  endString = endString +"ay";
                  startString = input.substring(k+2,l);
                  finalString = startString + endString;
                  //System.out.println(finalString);
                  return finalString;


          }

          if(s=='a'||s=='e'||s=='i'||s=='o'||s=='u'||s=='A'||s=='E'||s=='I'||s=='O'||s=='U'||s=='y'||s=='Y')//if its a vowel or "y" than executes commands below
          {

              endString = input.substring(0, k);//gets the letters before the vowel
              endString = endString + "ay";
              startString = input.substring(k,l);//gets the letters after the vowel
              finalString = startString + endString;
              //System.out.println(finalString);//prints the final result which is the combination of startString with endString
              //stops code after doing the above
              return finalString;

          }


          else if(k==l-1)//if its the end of the word
          {
              finalString = "ERROR";
              return finalString;

          }

         }
          System.out.println(finalString);
          return finalString;
}///////////////////////////////////

//   public static void process(String input)//will take care of the punctuation
//   {
//       String latin = "";
//          int i = 0;
//          while (i<input.length()) {
//
//            // Takes care of punctuation and spaces
//            while (i<input.length() && !isLetter(input.charAt(i))) {
//              latin = latin + input.charAt(i);
//              i++;
//            }
//            latin = latin + output(input);
//            System.out.println(latin);
//          }
//          
//    }


    public static void main(String[] args)
    {

        String str;//this will be the input string by the user
        Scanner scanner = new Scanner(System.in);//this scanner will register the input value
        System.out.println("Enter a Word: ");
        str = scanner.next();//stores the input string

        output(str);//outputs it using basic gramatical rules

    }

}
4

3 回答 3

1

您应该在方法的每个本地块中都有一个return语句。top-level如果你没有它,那么在顶层块return的每个内部都有一个声明。block等等。

让我们考虑一组最简单的情况 if - else if - else: -

if您需要从每个或块内返回您的字符串else,因为只有其中一个会执行。因此,如果您在其中一个中错过了 return 语句,那么很可能在block执行该语句时,它肯定会错过 return 语句。前提是您在结束时没有任何退货声明method

因此,基本上,您的 return 语句必须出现在每个块中,其执行不需要执行任何其他块,并且如果这些块涵盖了条件可能具有的所有可能性,那么您不需要在块。因为其中一个块肯定会执行。

此外,如果这些blocks不能涵盖某个条件(like if you are not having anelse的所有可能性for a set of if-else-if),那么您必须return在这些块之外有一个语句。因为,如果这些块都没有执行,那么method将错过 return 语句。

因此,例如,您可以看到以下代码集,涵盖了最可能的可能性:-

public String returnString() {
        if (..) {
             return "someString";

        } else if (...) {
             return "someString";

        } else {
             return "someOtherString";
        }
       // return statement here is not needed. Because at least `else` will execute
}

因此,至少其中一个if,else ifelse将始终执行。因此,您可以在其中添加 return 语句,并将 return 语句留在这些块之外。

但是,如果您的最后一个else块是else if,那么可能没有blocks执行。在这种情况下,您必须在这些块之后放置一个 return 语句。

public String returnString() {
        if (..) {
             return "someString";

        } else if (...) {
             return "someString";

        } else if (...){
             return "someOtherString";
        }
       // return statement here is needed. 
       // Because its possible that none of the blocks in `if-else` set get executed.
}

另一种可能性是,不是从每个块返回,您可以将 存储return value在某个局部变量中,并在所有块的末尾,将value局部变量的返回作为方法中的最后一条语句。

public String returnString() {
    int returnValue = 0;
    if (..) { returnValue = someValue; }
    else if(...) { returnValue = someOtherValue; }

    return returnValue;
}

注意: -您可以在代码中使用最后一种方式,因为您将return value. finalString因此,只需在方法的最后一行返回该字符串。

于 2012-10-29T16:43:33.967 回答
0
private static String output(String input)//processes the word using basic rules including the q and u rule
{

    //the string that will hold the value of the word entered by the user
    char s;//the first character of the string
    char m;
    int l = input.length();//determines the length of the string
    String endString;
    String startString;
    String finalString;//the final output
    String mtr;
    String lowercase;//the entered string all converted to lowercase

    for(int k =0;k<l;k++)//checks all letters in order to see which is a vowel
    {

        s = input.charAt(k);

    if(s == 'q'|| s=='Q' && input.charAt(k+1)=='u')//if the first vowel is a "u" and the letter before it is a "q"
    {


            endString = input.substring(0,k+2);//makes the endString also include u
            endString = endString +"ay";
            startString = input.substring(k+2,l);
            finalString = startString + endString;
            System.out.println(finalString);
            break;


    }

    if(s=='a'||s=='e'||s=='i'||s=='o'||s=='u'||s=='A'||s=='E'||s=='I'||s=='O'||s=='U'||s=='y'||s=='Y')//if its a vowel or "y" than executes commands below
    {

        endString = input.substring(0, k);//gets the letters before the vowel
        endString = endString + "ay";
        startString = input.substring(k,l);//gets the letters after the vowel
        finalString = startString + endString;
        System.out.println(finalString);//prints the final result which is the combination of startString with endString
        break;//stops code after doing the above

    }


    else if(k==l-1)//if its the end of the word
    {
        System.out.println("ERROR");
        break;
    }
   }
   return finalString;
} 

这里。尝试这个 :-/

我不知道你想做什么。如果您跳出循环,您将超出for()or while()ordo..while()循环,在您的情况下,这几乎是该方法的结束。我将 return finalString 放在那里,并将方法的返回类型outputvoid更改为String

于 2012-10-29T16:36:21.217 回答
0

抱歉,这不是一个真正的答案,Rohit Jain 很好地解释了为什么您的代码不起作用。从那里你应该能够弄清楚。

看着你的代码,我觉得这段代码比它需要的复杂得多。当我将代码粘贴到其中时,我的 IDE(Eclipse)会警告我未使用的变量。正如您的案例所述,拥有复杂的代码是导致问题的原因。

我认为在您的情况下,进行一些重构会有所帮助。而且你没有问题。为了让您开始,请尝试将寻找将字符串一分为二的位置和实际切割的问题分开。那应该有帮助。也许是这样的:

private static int firstVowel(String input) {
    for (int i = 0; i < input.length(); i++) {
        char aChar = input.charAt(i);
        if ("aeiouyAEIOUY".indexOf(aChar) >= 0) {
            if (aChar == 'u' && i > 0 && Character.toLowerCase(input.charAt(i-1)) == 'q') {
                return i-1;
            }
            // else
            return i;
        }
    }
    // if we get here no vowel was found
    return -1;
}

// /////////////////////////////////////////
private static String output(String input)
{
    int firstVowel = firstVowel(input);
    if (firstVowel < 0) {
        return "ERROR";
    }
    // else
    String start = input.substring(firstVowel);
    String end = input.substring(0, firstVowel) + "ay";
    return start + end;
}// /////////////////////////////////


public static void main(String[] args) {

    String str;// this will be the input string by the user
    Scanner scanner = new Scanner(System.in);// this scanner will register
                                                // the input value
    System.out.println("Enter a Word: ");
    str = scanner.next();// stores the input string

    System.out.println(output(str));// outputs it using basic gramatical rules

}
于 2012-10-29T21:03:29.353 回答