自己定义计算阶乘的类,编译后没有问题,生成Factorial4.classpackage 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);
}
}下面是一个计算阶乘的程序,其中调用了Factorial4package 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

解决方案 »

  1.   

    是不是没有把Factorial4.class import进来?
      

  2.   

    import com.davidflanagan.examples.basics.Factorial4;
      

  3.   

    System.out.println(x + " != " +Factorial4.factorial(x));
    改成
    System.out.println("" + x + " != " +Factorial4.factorial(x));
      

  4.   

    danjiewu(阿丹)没有变化啊,错误还是一样的
    而且我也不觉得有什么区别
      

  5.   

    你在一个类中调用另一个类的方法,不做另一个类的实例化对象怎么去调用啊
    例:
       Factorial4 f4=new Factorial4();
    System.out.println(x + " != " +Factorial4.factorial(x));
    改成
    System.out.println("" + x + " != " +f4.factorial(x));
    试试
      

  6.   

    另外2个类要放在同一个工程里。
    如果不是在同一个工程里,在编译FactQuoter.java时要把Factorial4.class所在路径添加到Classpath里,在运行FactQuoter.class时同样也要把Factorial4.class所在路径添加到Classpath里。System.out.println(x + " != " +Factorial4.factorial(x));
    编译是肯定通不过的,int型不能与String型相加,反过来可以。
      

  7.   

    danjiewu(阿丹) 唉,我真的是个初学者,我使用的编程工具是editplus,还没有创建工程呢!
      

  8.   

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

  9.   

    public class FactQuoter
    {
    public static void main(String[] args) throws IOException
    {
    BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
    Factorial4 f4=new Factorial4();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 + " != " + f4.factorial(x));
    }
    catch (Exception e)
    {
    System.out.println("Invalid Input");
    }}
    }
    }
      

  10.   

    你的程序不需要进行任何修改,我认为是classpath的问题,在我机器上运行没有任何问题,编译时就只有两个警告
      

  11.   

    String也重载了前置+号,我还不知道,失败。
    System.out.println(x + " != " +Factorial4.factorial(x))没有问题的,只要加classpath就可以了。
      

  12.   

    :( classpath 是在环境变量里设置吗?
    我这里不行啊!
      

  13.   

    CLASSPATH=.;C:\rpms\jdk1.4\lib\tools.jar;F:\java\Factorial4.class
    是这样吗?
      

  14.   

    这些代码在我机器上面编译运行都是正常的啊
    会不会jdk版本低了?