java - return 跳出小问题 达人们 救救菜鸟~
来源:百度知道 编辑:UC知道 时间:2024/06/23 08:05:29
import java.io.*;
class digui
{
int x;
int s;
static int theInteger=0;
int digui(int s)
{
if(s<0)
{
System.out.println("输入错误!");
return x=-1;
}
if(s==0)
{
return x=1;
}
else
{
x=digui(s-1)*s;
return x;
}
}
public static void main(String[] args) throws IOException
{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
String inputstring=input.readLine();
theInteger=Integer.parseInt(inputstring);
digui dg =new digui();
dg.digui(theInteger);
}
}
出现的错误是 missing retuing statement 就是说IF里面的return 只跳出IF 了。但是没给digui() 返回值。~ 问:怎么跳出。。应该怎么写。
还有就是theInteger 我上面非要声明为static 才可以。为什么呢??
达人给解答~~ 小弟拜谢~~。。
class digui
{
int x;
int s;
static int theInteger=0;
int digui(int s)
{
if(s<0)
{
System.out.println("输入错误!");
return x=-1;
}
if(s==0)
{
return x=1;
}
else
{
x=digui(s-1)*s;
return x;
}
}
public static void main(String[] args) throws IOException
{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
String inputstring=input.readLine();
theInteger=Integer.parseInt(inputstring);
digui dg =new digui();
dg.digui(theInteger);
}
}
出现的错误是 missing retuing statement 就是说IF里面的return 只跳出IF 了。但是没给digui() 返回值。~ 问:怎么跳出。。应该怎么写。
还有就是theInteger 我上面非要声明为static 才可以。为什么呢??
达人给解答~~ 小弟拜谢~~。。
最后一句改成
System.out.println (dg.digui (theInteger));
你都没有把结果输出
后面改下不就行了啊.int x=dg.digui(theInteger);返回值赋给变量System.out.println(x);theInteger是成员变量,你没有实例化,是不能使用的.