我想返回字符串“电气”与用户输入的数字。我已经创建了这个程序。问题是它返回的是0,而不是只显示输出。我知道问题出在哪里,我只是不知道正确的解决办法。
例子:
投入:3
产出:
电动的 电动的 电动的 0 <-这里不应该有零。
import java.io.*;
public class quitx{
public static BufferedReader v = new BufferedReader(new InputStreamReader(System.in));
public static int s;
public static void main(String[] args) throws Exception{
System.out.println("Enter an integer : ");
s = Integer.parseInt(v.readLine());
System.out.println(x(s));
}
public static int x(int s){
if(s <= 0)
return s;
else{
System.out.println("Electric!");
return x (s - 1);
}
}
}发布于 2014-10-19 14:22:55
x()方法返回一个int。你不希望这个int被打印出来。但你却在打电话
System.out.println(x(s));如果你不想把结果打印出来那.不要打印:
x(s);发布于 2014-10-19 14:24:15
您的方法总是返回0,因此,与其打印x(s),只需调用x(s),然后打印s(不确定是否真的要在末尾打印)。
发布于 2014-10-19 14:22:56
删除方法的返回类型:
public static void x(int s){
if (s > 0) {
System.out.println("Electric!");
x(s - 1);
}
}并将呼叫更改为
x(s);https://stackoverflow.com/questions/26451437
复制相似问题