public class Pra001 {
double acceleration;
double speed;
public static void main(String[] args) {
}
public static double getSpeed(double distance, double time) {
double speed = distance / time;
return speed;
}
public static double getAcceleration(double distance, double time) {
double acceleration=(2*distance)/(time*time);
return acceleration;
}
}
贴上代码,大家可以就着这个代码来回答。。
5 个解决方案
#1
艹。。。又是你
#2
不会调试吗?在考试吗?
#3
没影响,但稍微正规点项目都会扫描这种代码,反正这种代码是无法提交到SVN的
不要鄙视俺们还在使用SVN
不要鄙视俺们还在使用SVN
#4
还是你?醉了,以前不是说过了,方法的形参不会影响到实参的,getSpeed(double distance, double time)比如这个方法,虽然你里面有speed但是你这个字段前面有个类型double,这就相当于在方法里面重新声明了一个speed,这个和外面的成员变量speed是没有关系的,所以如果调用这个方法有个返回值,但是pra001.speed 其实还是0,除非,你将方法里面的speed的double类型去掉,不过因为你这个方法是static修饰的,所以外面的变量也要加static或者这个方法将static去掉
#5
public class Pra001 {
double speed=0;
double acceleration=0;
public static void main(String[] args) {
Pra001 obj=new Pra001();
System.out.println("Before testing, speed = "+obj.speed+" and acceleration = "+obj.acceleration );
System.out.println("After testing, sped = "+obj.getSpeed(10, 2)+" and acceleration = "+Pra001.getAcceleration(10, 1));
System.out.println("Have a check that, the field: speed ="+obj.speed+" and acceleration = "+obj.acceleration);
}
public double getSpeed(double distance, double time) {
this.speed = distance / time;
return this.speed;
}
public static double getAcceleration(double distance, double time) {
double acceleration = (2 * distance) / (time * time);
return acceleration;
}
}
输出结果:
Before testing, speed = 0.0 and acceleration = 0.0
After testing, sped = 5.0 and acceleration = 20.0
Have a check that, the field: speed =5.0 and acceleration = 0.0
Bingo!
#1
艹。。。又是你
#2
不会调试吗?在考试吗?
#3
没影响,但稍微正规点项目都会扫描这种代码,反正这种代码是无法提交到SVN的
不要鄙视俺们还在使用SVN
不要鄙视俺们还在使用SVN
#4
还是你?醉了,以前不是说过了,方法的形参不会影响到实参的,getSpeed(double distance, double time)比如这个方法,虽然你里面有speed但是你这个字段前面有个类型double,这就相当于在方法里面重新声明了一个speed,这个和外面的成员变量speed是没有关系的,所以如果调用这个方法有个返回值,但是pra001.speed 其实还是0,除非,你将方法里面的speed的double类型去掉,不过因为你这个方法是static修饰的,所以外面的变量也要加static或者这个方法将static去掉
#5
public class Pra001 {
double speed=0;
double acceleration=0;
public static void main(String[] args) {
Pra001 obj=new Pra001();
System.out.println("Before testing, speed = "+obj.speed+" and acceleration = "+obj.acceleration );
System.out.println("After testing, sped = "+obj.getSpeed(10, 2)+" and acceleration = "+Pra001.getAcceleration(10, 1));
System.out.println("Have a check that, the field: speed ="+obj.speed+" and acceleration = "+obj.acceleration);
}
public double getSpeed(double distance, double time) {
this.speed = distance / time;
return this.speed;
}
public static double getAcceleration(double distance, double time) {
double acceleration = (2 * distance) / (time * time);
return acceleration;
}
}
输出结果:
Before testing, speed = 0.0 and acceleration = 0.0
After testing, sped = 5.0 and acceleration = 20.0
Have a check that, the field: speed =5.0 and acceleration = 0.0
Bingo!