.\sphere.java:16: cannot resolve sym symbol : variable radius location: class sphere return sphere.PI*radius*x*y*z; ^ .\sphere.java:16: cannot resolve sym symbol : variable x location: class sphere return sphere.PI*radius*x*y*z;
.\sphere.java:16: cannot symbol : variable y location: class sphere return sphere.PI*radius*x*y*z; ^ .\sphere.java:16: cannot resolve sym symbol : variable z location: class sphere return sphere.PI*radius*x*y*z; ^还是不行啊,继续请教了
symbol : variable radius
location: class sphere
return sphere.PI*radius*x*y*z;
^
.\sphere.java:16: cannot resolve sym
symbol : variable x
location: class sphere
return sphere.PI*radius*x*y*z;
.\sphere.java:16: cannot
symbol : variable y
location: class sphere
return sphere.PI*radius*x*y*z;
^
.\sphere.java:16: cannot resolve sym
symbol : variable z
location: class sphere
return sphere.PI*radius*x*y*z;
^还是不行啊,继续请教了
static final double PI=3.1415926;
static int count=0;
double xcenter;
double ycenter;
double zcenter;
double radiu; ----> double radius;
sphere(double radius,double x,double y,double z){
radius=radiu; ----> this.radius = radius;
xcenter=x;
ycenter=y;
zcenter=z;
++count;
}
double vol(){
return PI*radius*x*y*z;
}
public static void main(String[] args){
sphere nn=new sphere(10,20,1.3,5);
System.out.println(nn.vol());
}
}
By:风之子,你的“你在非static函数里调用了static变量,将double vol()改成static double vol()就可以了”这个说法不能成立哦,拜托各位老大,请继续,出错的代码我也贴出来了,出错就在这4个变量上,拜托拜托
class sphere{
static final double PI=3.1415926;
static int count=0;
double xcenter;
double ycenter;
double zcenter;
double radiu;
sphere(double radius,double x,double y,double z){
radius=radiu; //radiu是成员变量,没有赋值则java默认初值0.0,这是你想要的吗?
xcenter=x;
ycenter=y;
zcenter=z;
++count;
}
double vol(){
return PI*radius*x*y*z; //错误1:x,y,z是sphere类构造方法的参数,能在这里调用吗?
}
public static void main(String[] args){
sphere nn=new sphere(10,20,1.3,5);
System.out.println(nn.vol());
}
}
class sphere{
static final double PI=3.1415926;
static int count=0;
double xcenter;
double ycenter;
double zcenter;
double radiu;
sphere(double radius,double x,double y,double z){
radiu=radius;
xcenter=x;
ycenter=y;
zcenter=z;
++count;
}
double vol(){
return PI*radiu*xcenter*ycenter*zcenter;
}
public static void main(String[] args){
sphere nn=new sphere(10,20,1.3,5);
System.out.println(nn.vol());
}
}
static final double PI=3.1415926;
static int count=0;
double xcenter;
double ycenter;
double zcenter;
double radiu;
sphere(double radius,double x,double y,double z){
radius=radiu;
xcenter=x;
ycenter=y;
zcenter=z;
++count;
}
double vol(){
return PI*radius*x*y*z;
}
public static void main(String[] args){
sphere nn=new sphere(10,20,1.3,5);
System.out.println(nn.vol());
}
}
sphere(double radius,double x,double y,double z){
radius=radiu;
xcenter=x;
ycenter=y;
zcenter=z;
++count;
}
double vol(){
return PI*radius*x*y*z;
我自己也试出来了,但是我就是没明白为什么我按书上的做就是不行?我是新手,让大家见笑了