如何将void语句转换为返回字符串的return语句?

z9smfwbn  于 2021-06-24  发布在  Pig
关注(0)|答案(3)|浏览(379)

好吧,我试着把下面的语句转换成返回语句 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

    }

}
jutyujz0

jutyujz01#

你应该有一个 return 在每个 top-level 方法中的本地块。如果你没有,那就来一杯 return 声明,在每个 block 在顶层大楼里。等等。
让我们考虑一组 if - else if - else : -
你需要从每个 if 或者 else 阻塞,因为只有其中一个将执行。所以,如果你错过了其中一个的return语句,那么当 block 执行时,则肯定会丢失return语句。前提是你在你的期末没有任何返回声明 method 因此,基本上,您的return语句必须出现在每个块中,它的执行不需要执行任何其他块,如果这些块包含了条件可能具有的所有可能性,那么您不需要块之外的return语句。因为其中一个块肯定会执行。
还有,如果那些 blocks 不包括某种情况的所有可能性 (like if you are not having an 其他的 for a set of if-else-if) ,那么你必须有一个 return 在那些街区外的声明。因为,如果这些块都不执行,那么 method 将错过返回语句。
因此,例如,您可以看到以下代码集,涵盖了最可能的可能性:-

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 if 或者 else 将始终执行。因此,您可以在其中添加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 valuefinalString . 所以,只需在方法的最后一行返回该字符串。

b5buobof

b5buobof2#

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() 或者 while() 或者 do..while() 循环,在您的例子中,这将几乎是方法的结尾。我将return finalstring放在那里,还更改了方法的返回类型 outputvoidString

vawmfj5a

vawmfj5a3#

抱歉,这并不是一个真正的答案,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

}

相关问题