返回方法值的类

时间:2014-02-23 00:55:38

标签: java class methods

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,它不起作用。为什么不工作?它只打印原始速度。

7 个答案:

答案 0 :(得分:2)

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

double accerlate = ++speed; ==> accerlate = speed + 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)

==>为了得到它,请声明一个双倍:Double ouputSpeed=mycar.accerlate();然后打印它。

答案 1 :(得分:0)

这就够了

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

答案 2 :(得分:0)

如上所述,请改用++accelerate

然而,由于这个

,目前的程序将始终打印速度50
double speed = 50;
mycar.output(speed);

然后output()的参数直接传递给该方法中的最后一个System.out.println()。此外,主要调用getYear()getMake()getSpeed()实际上并没有做任何事情。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

这可以解决您的问题:

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

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

答案 5 :(得分: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);
}
}

答案 6 :(得分:-1)

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

++与+1不同,++表示操作在分配值后发生,但+1会在发生之前发生,也可能是++速度。