import java.io.*; import java.lang.*;
public class basics{
String input()throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String type=br.readLine();
return type;
}
String work(String S){
int whitespace; int count=0; String wrd=new String[5]
int lenghth=S.length();
int t=0;
while(t<length){
if(S.charAt(t)=='') {
whitespace++;
count++;
}
else{
wrd[count]+=S.charAt(t);
}
Random rand = new Random();
int n;
String blank="";
for(int j=1;j<6;j++){
n = rand.nextInt(4) + 0;
blank.append(wrd[n]);
}
return blank;
}
}
public static void main(String args[])throws IOException{
System.out.println("enter a sentence of five words");
String x; x.input();
String output=work(x);
System.out.println(output);
}
}
这段代码是混淆五个单词的单词并产生输出,问题就是这个方法不能返回字符串,该怎么办?
答案 0 :(得分:0)
正确缩进代码后,您可以看到return
方法中的while(t<length)
之外没有work
语句。如果该情况没有输出,则返回空字符串或抛出异常。
String work(String S){
int whitespace; int count=0; String wrd=new String[5]
int lenghth=S.length();
int t=0;
while(t<length){
if(S.charAt(t)=='') {
whitespace++;
count++;
}
else{
wrd[count]+=S.charAt(t);
}
Random rand = new Random();
int n;
String blank="";
for(int j=1;j<6;j++){
n = rand.nextInt(4) + 0;
blank.append(wrd[n]);
}
return blank;
}
//HERE
return ""; // or return a proper default value
}