public class Gas extends Car{
public Gas (String wheels, String frame, double engine, int maxSpeed) {
super (wheels, frame, engine, maxSpeed);
for (int speed = 0; speed<maxSpeed; speed++) {
for (double gasLiter= 60; gasLiter <60; gasLiter-=0.25){
答案 0 :(得分:1)
您需要一个循环:
double gasLiter = 60;
for (int speed = 0; speed < 800; speed++, gasLiter-=0.25) {
}
您还可以添加第二个停止条件:
double gasLiter = 60;
for (int speed = 0; speed < 800 && gasLiter >= 0; speed++, gasLiter-=0.25) {
}
我认为您原来的gasLiter < 60
条件是一个错误。
答案 1 :(得分:1)
for (int speed = 0; double gasLiter = 60; speed < maxSpeed; gasLiter>0; speed++, gasLiter-=0.25) {
// your code
}
答案 2 :(得分:0)
@Eran已解决问题,但您还可以添加条件,以在没有更多气体可用时停止:
double gasLiter = 60;
for (int speed = 0; speed < 800 && gasLiter > 0; speed++, gasLiter-=0.25) {
// ^ initial conditions; ^ end conditions ; ^ increment speed, decrement gas
}
答案 3 :(得分:0)
您可以添加一个检查气体是否达到0.0。
int maxSpeed = 200;
double gasLiter = 60;
for (int speed = 0; speed < maxSpeed; speed++) {
gasLiter -= 0.25;
}