import myPack.*;
public class HelloWorld{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
Factorial f= new Factorial();
int d = f.fact(a);
System.out.println("Factorial of " +a+ " is : " +d);
}
}
package myPack;
public class Factorial
{
public int fact(int b)
{
int c=1;
for(int i=b;i>0;i--)
{
c=c*i;
}
return c;
}
}
E:\Packages>javac HelloWorld.java
HelloWorld.java:7: error: cannot access Factorial
Factorial f= new Factorial();
^
bad source file: .\Factorial.java
file does not contain class Factorial
Please remove or make sure it appears in the correct subdirectory of the sou
rcepath.
1 error
答案 0 :(得分:1)
将文件Factorial.java
放在名为myPack
的文件夹中。
然后使用:javac HelloWorld.java
在java中,文件结构必须遵循包。有关详细信息,请参阅此tutorial。