自己定义计算阶乘的类,编译后没有问题,生成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
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
改成
System.out.println("" + x + " != " +Factorial4.factorial(x));
而且我也不觉得有什么区别
例:
Factorial4 f4=new Factorial4();
System.out.println(x + " != " +Factorial4.factorial(x));
改成
System.out.println("" + x + " != " +f4.factorial(x));
试试
如果不是在同一个工程里,在编译FactQuoter.java时要把Factorial4.class所在路径添加到Classpath里,在运行FactQuoter.class时同样也要把Factorial4.class所在路径添加到Classpath里。System.out.println(x + " != " +Factorial4.factorial(x));
编译是肯定通不过的,int型不能与String型相加,反过来可以。
{
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");
}}
}
}
System.out.println(x + " != " +Factorial4.factorial(x))没有问题的,只要加classpath就可以了。
我这里不行啊!
是这样吗?
会不会jdk版本低了?