读书人

菜鸟问个简单有关问题关于调用自编写

发布时间: 2012-01-26 19:40:46 作者: rapoo

初学者问个简单问题,关于调用自编写类的问题
自己定义计算阶乘的类,编译后没有问题,生成Factorial4.class

package com.davidflanagan.examples.basics;
import java.math.BigInteger;
import java.util.*;

public class Factorial4
{
protected static ArrayList table = new ArrayList();
static{
table.add(BigInteger.valueOf(1));
}
public static synchronized BigInteger factorial(int x)
{
if (x < 0)
{
throw new IllegalArgumentException( "x must be non-negative ");
}
for (int size = table.size();size <= x ;size++ )
{
BigInteger lastfact = (BigInteger)table.get(size - 1);
BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(size));
table.add(nextfact);
}
return (BigInteger) table.get(x);
}
}

下面是一个计算阶乘的程序,其中调用了Factorial4

package com.davidflanagan.examples.basics;
import java.io.*;

public class FactQuoter
{
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
for (; ; )
{
System.out.print( "FactQuoter > ");
String line = in.readLine();
if ((line == null) || line.equals( "quit ")) break;
try
{
int x = Integer.parseInt(line);
System.out.println(x + " != " + Factorial4.factorial(x));
}
catch (Exception e)
{
System.out.println( "Invalid Input ");
}

}
}
}
编译错误 :FactQuoter.java:26: cannot resolve symbol
symbol : variable Factorial4
location:class com.davidflanagan.examples.basics.FactQuoter
System.out.println(x + " != " +Factorial4.factorial(x));

1 error


[解决办法]
是不是没有把Factorial4.class import进来?
[解决办法]
import com.davidflanagan.examples.basics.Factorial4;
[解决办法]
实例化引用
[解决办法]
System.out.println(x + " != " +Factorial4.factorial(x));
改成
System.out.println( " " + x + " != " +Factorial4.factorial(x));

[解决办法]
java的classpath设定对吗?有没有将当前路径加入,我把这两个类写到一起没有问题的
[解决办法]
..............................

[解决办法]
这些代码在我机器上面编译运行都是正常的啊


会不会jdk版本低了?

读书人网 >J2SE开发

热点推荐