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);
}
}
我写的代码:
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);
}
}
解决方案 »
- 问个跟java没多大关系的不过很要命的问题,希望知道的朋友的提点下
- jtable中插入jtextfield问题
- 关于DataInputStream的问题
- JPanel中的paintComponent方法是用来做什么的?
- 各位高手,请帮我注释一下代码。
- 又长一个角了高兴在放分,
- 急问各位:有没有了解installanywhere的呢?有个小问题要请教!
- sql 中获取_下划线后的内容。
- 在java中只用一个Connection ,如何保证我执行多条sql语句,其中有几条失败而不回滚全部,我只需要让他回滚这几条
- 问高手们的一道题,不用控件,不用java2,如何实现画半透明色的图形
- Hashtable用法?
- 一段代码,关于数组排序的.
最小的是12
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();
}}
}
错误:没有声明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);
}
}
修改后的代码:
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));
}
}
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);
}
}}