日期:2014-05-20  浏览次数:20744 次

初学者问个简单问题,关于调用自编写类的问题
自己定义计算阶乘的类,编译后没有问题,生成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版本低了?