JAVA菜鸟问题

minerchow 2008-04-25 09:33:07
1号比2号大2岁,2号比3号大2岁,3号比4号大2岁...........6号比7号大2岁,求1号年龄?
我写的代码:
import java.io.*;
class factorial {
int fact(int n) {
int a;
if(n==1) return 12;
a=fact(n-1)+2;
return a;
}
}
class re{
factorial f=new factorial();
public static void main(String[] args)throws IOException
{String str;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readline();
n=Interger.parseInt(Str);
System.out.println(n);
}
}

...全文
139 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
mike123hl 2008-04-27
  • 打赏
  • 举报
回复
呵呵 递归不错啊
sk8811098 2008-04-27
  • 打赏
  • 举报
回复
大哥,你的语法有错误啊,要调用f变量要在static里面定义才能调用,抛出异常的语法也有错,我改了一下,执行通过答案正确。
import java.io.*;
public class test{
int fact(int n) {
int a;
if(n==1) return 12;
a=fact(n-1)+2;
return a;
}
public static void main(String[] args)
{
try
{
test f=new test();
String str;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine(); int n;
n=Integer.parseInt(str);
System.out.println(f.fact(n));
}
catch(IOException ex)
{
System.err.println(ex);
}
}}
ly290814 2008-04-27
  • 打赏
  • 举报
回复
少条件吧,不打明白
minerchow 2008-04-27
  • 打赏
  • 举报
回复
最小的12岁,我想随机输入一个数,获得1号年龄,有些问题已经修改过了.但还是有问题.
修改后的代码:
import java.io.*;
class factorial {
int fact(int n) {
int a;
if(n==1) return 12;
a=fact(n-1)+2;
return a;
}
}
class re{
factorial f=new factorial();
public static void main(String[] args)throws IOException
{String str;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine(); int n;
n=Interger.parseInt(str);
System.out.println(f.fact(n));
}
}
miracle335 2008-04-26
  • 打赏
  • 举报
回复
我也是菜鸟哦~不知道说得对不对~不过我自己修改过楼主的代码
错误:没有声明n这一变量.
2.readline()改成readLine() //java是区分大小写的~楼主的readline()中的l要大写,readLine()
3.n=Interger.parseInt(Str); //楼主声明的str中的s是小写的,但这行代码Str中S是大写的,把他改回小写.
4.n=Interger.parseInt(str); //其中interger关键字写错,应该是integer


你声明了一个factorial 并创建了它的对象,但最后却没有调用。(这个不是错误)

以下是我改了的代码~能通过编译,
import java.io.*;
class factorial
{
int fact(int n)
{
int a;
if(n==1) return 12;
a=fact(n-1)+2;
return a;
}
}

class re
{
public static void main(String[] args)throws IOException
{
factorial f=new factorial();
String str;
int n,a; //声明n,a
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
str=br.readLine(); //把l改成大写L
n=Integer.parseInt(str); //interger关键字错误,应是integer
//以下是调用fact mouth
a=f.fact(n);
System.out.println(a);
}
}
goodmrning 2008-04-26
  • 打赏
  • 举报
回复
可以参考下:
import java.io.*;
class factorial {
int fact(int n) {
int a;
if(n==1) return 12;
a=fact(n-1)+2;
return a;
}
}
class re{
factorial f=new factorial();
public static void main(String[] args)
{try {
Expression f=new Expression();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
System.out.println("n="+n);
System.out.println("1号年龄是:"+f.fact(n));

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}}
}
tganthem 2008-04-26
  • 打赏
  • 举报
回复
if(n==1) return 12;
最小的是12
gshywx 2008-04-26
  • 打赏
  • 举报
回复
我都看晕了,我也认为应该知道7号多大
希望搂住的问题早日解决
richard_2010 2008-04-26
  • 打赏
  • 举报
回复
7号多大?这才是关键哦
KKK2007 2008-04-26
  • 打赏
  • 举报
回复
写的不错,用递归酸法
huanggeng_8552 2008-04-26
  • 打赏
  • 举报
回复
麻烦楼主把问题说清楚点,你这样是没有任何人能给出答案的.
miracleliu 2008-04-25
  • 打赏
  • 举报
回复
7号多大?应该告诉吧
MagiKing 2008-04-25
  • 打赏
  • 举报
回复
????????????

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧