[英]How to convert void statement into return statement returning a string?
好的,所以我尝试将下面的语句转换为返回finalString
的return语句,但是即使我确实返回finalString
“此语句必须返回String类型的变量”,它也始终告诉我。 我试过将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
}
}
您的方法中的每个top-level
本地块中都应该有一个return
语句。 如果没有,则在该顶级block
内的每个block
都有一个return
语句。 等等。
让我们考虑一组 if - else if - else
的最简单情况 :-
您需要从每个if
or else
块内部返回您的字符串,因为只有其中一个会执行。 因此,如果您错过其中之一的return语句,那么当执行该block
时,肯定会错过return语句。 如果您在method
末尾没有任何return语句
因此,基本上,您的return语句必须位于每个块中,每个块的执行都不需要执行任何其他块,并且如果这些块涵盖了条件可能具有的所有可能性,则您不需要在语句之外的return语句。块。 因为这些块之一肯定会执行。
同样,如果这些blocks
不能覆盖某个条件的所有可能性(like if you are not having an
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 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 value
存储在finalString
。 因此,只需在方法的最后一行中返回该字符串即可。
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放在这里,并且还将方法output
的返回类型从void
更改为String
抱歉,这不是真正的答案,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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.