2014-02-23 134 views
1
public class car 
{ 
private int model; 
private String make; 
private double speed; 
/** 
* * My constructor 
*/ 
public car() 
{ 
    model = 2000; 
    make= "Ferrari"; 
    speed= 50; 

} 

public int getYear() 
{ 
    return model; 
} 

public String getMake() 
{ 
    return make; 
} 

public double getSpeed() 
{ 
    return speed; 
} 

public double accerlate() 
{ 
    double accerlate = speed++; 
    return accerlate; 
} 

public void output(double accerlate) 
{ 
    System.out.println("Year: " +model); 
    System.out.println("Make: " + make); 
    System.out.println("Speed: " + speed); 
    System.out.println(accerlate); 
} 
} 

连接到这个类类返回的方法值

public class RaceTrack 
{ 
public static void main(String args[]){ 
    double speed = 50; 
    car mycar= new car(); 
    mycar.getYear(); 
    mycar.getMake(); 
    mycar.getSpeed(); 
    mycar.output(speed); 
    mycar.accerlate(); 
} 
} 

不管怎么说,我试图让一个输出和accerlate方法不工作,我想要的方式。它应该是+1并且它不起作用。为什么它不工作?它只是打印原始速度。

回答

2

double accerlate = speed++; ==> accerlate =速度则速度=速度+ 1

double accerlate = ++speed; ==> accerlate =速度+ 1

对于您的代码: 你的输出将是:

Year: 2000 
Make: Ferrari 
Speed: 50.0 
50.0 

这是mycar.output(speed);的输出其他方法还没输出你主

double speed = 50; 
car mycar= new car(); 
mycar.getYear(); //getYear return an int (model)=2000 so store it if you want 
mycar.getMake(); //getMake return a string 
mycar.getSpeed(); //getSpeed return a double =50 

,如果你想看到加快+1,你应该得到的方法的返回然后打印:

Double ouputSpeed=mycar.accerlate(); 
    System.out.println(ouputSpeed.toString()); // print 51.0 

返回:

public double accerlate() 
{ 
    double accerlate = ++speed; 
    return accerlate; 
} 

==>这个方法的返回一个double(accerlate = spped + 1 = 51)

==>为了让它在你的main中,声明一个double:Double ouputSpeed=mycar.accerlate();然后打印它。

+0

它的工作,谢谢 – user3296193

+1

欢迎您:) –

-1
public double accerlate() 
{ 
    double accerlate = speed+1; 
    return accerlate; 
} 

++比+1不同,++指操作发生在价值的分配之后,但是+1会发生手之前,还可以做++速度。

+0

它不是做法,accerlate是一样的速度。 – user3296193

0

这就够了

public double accerlate() 
{ 
return ++speed; 
} 
0

如前所述,使用++accelerate代替。

然而,该方案因为它代表将始终打印,因为这个

double speed = 50; 
mycar.output(speed); 

那么output()的说法是,该方法中直接传递到最后System.out.println()速度50。此外,主要调用getYear()getMake()getSpeed()实际上并没有做任何事情。

0

在Java中编写方法时,必须注意的一件事是将值存储到变量中,然后返回这些变量。返回变量可能会导致数据过时。换句话说,java认为像int或double这样的东西没有价值。无论如何要防止这种情况发生,特别是在增加或减少这些值时,要确保使用零值等初始化这些变量。你的代码中没有这样做。这可能就是为什么

0

这将解决你的问题:

public double accerlate() 
{ 
    return ++speed; 
} 

另外,我注意到,你在你的main方法创建的speed变量。我不确定你需要那个。

0

好的,var double speed = 50;在方法的地方,而这将是全球

public class car 
{ 
private int model; 
private String make; 
private double speed; 
private double accerlate // you need make the getter and setter 
/** 
* * My constructor 
*/ 
public car() 
{ 
    model = 2000; 
    make= "Ferrari"; 
    speed= 50; 

} 

public int getYear() 
{ 
    return model; 
} 

public String getMake() 
{ 
    return make; 
} 

public double getAccelerate() 
{ 
return accelerate; 
} 
public double setAccelerate(double accelerate) 
{ 
    this.accelerate = accelerate; 
} 

public double getSpeed() 
{ 
    return speed; 
} 

public double accerlate() 
{ 
    accerlate = speed++; 
    return accerlate; 
} 

public void output(double accerlate) 
{ 
    System.out.println("Year: " +model); 
    System.out.println("Make: " + make); 
    System.out.println("Speed: " + speed); 
    System.out.println(accerlate); 
} 
}