开发者

A double local variable is printed as 0.0 instead of the set value

开发者 https://www.devze.com 2023-03-03 14:10 出处:网络
I\'m having trouble with printing out the variable priceUsed After the methodgetPriceAfterUse() get the value for me, the method OutputDetails should print all the information. But the variable priceU

I'm having trouble with printing out the variable priceUsed After the method getPriceAfterUse() get the value for me, the method OutputDetails should print all the information. But the variable priceUsed is printed 0.0. I don't know why.

Here is my code:

    import java.util.Scanner;
 class Car
{
public String brandName;
public String color;
public double priceNew, priceUsed;
public double odometer;

public double getPriceAfterUse()
{
     priceUsed = priceNew*(1-(odometer/6000000));
     return priceUsed;

}

public double updateMilage()
{
    Scanner keyboard = new Scanner(System.in);
    odometer = keyboard.nextDouble();
    return odometer;

}
public void outputDetails()
{
    System.out.println("The car brand name is : " + brandName);
    System.out.println("The car new price: " + priceNew);
    System.out.println("The car used price: " + priceUsed);
    System.out.println("The car color: " + color);
    Sys开发者_运维问答tem.out.println("The car Odemeter: " + odometer );
}


}

public class CarTest{

public static void main(String args[])
    {
Scanner keyboard = new Scanner (System.in);

Car a = new Car();
System.out.println("Enter you car Brand Name: ");
a.brandName = keyboard.next();
System.out.println("Enter your car color: ");
a.color = keyboard.next();
System.out.println("Enter your new price: ");
a.priceNew = keyboard.nextDouble();
System.out.println("Enter your Odometer:");
a.updateMilage();
System.out.println();
a.outputDetails();
System.out.println();


 }
 }


public double getPriceAfterUse()
{
    priceUsed = priceNew*(1-(odometer/6000000));
    return priceUsed;
}

Only this method sets the value of priceUsed, and it isn't called until after outputDetails(), so that's the reason you're not getting the correct result.

However, there's a more important problem here: as a rule of thumb, methods called getSomething() shouldn't be doing anything other than return a value. They certainly must not have any side effects visible from the outside.

Even more generally, methods should have names which describe what they do as closely as possible. Misleading method names can cause you a lot of pain in the future.

Another thing you should look out for is to try to keep your objects in a consistent state. As you've noticed, priceUsed isn't automatically updated when mileage is, even though it is a function of mileage. It is also a function of priceNew, which can also be set separately, so unless you call every method in the correct order, you'll get an inconsistent state. The solution to this is to have a single operation that updates all of them at once:

public void updateValue( double priceNew, double odometer ) {
  this.priceNew = priceNew;
  this.odometer = odometer;
  this.priceUsed = priceNew * (1.0d - ( odometer / 6000000.0d ) );
}


You are calling OutputDetails before you are setting the value of PricedUsed.

If you look at the output on the last line you have

System.out.println(a.getPriceAfterUse());

This works correctly because it it the first time it is called. Maybe you could add getPriceAfterUse() call to the OutputDetials method so that the used price is updated each time you display the values.

EDIT:

To solve this problem you could do this.

  public void outputDetails() {
    //Calculate the used price before outputting the value
    getPriceAfterUse()
    System.out.println("The car brand name is : " + brandName);
    System.out.println("The car new price: " + priceNew);
    System.out.println("The car used price: " + priceUsed);
    System.out.println("The car color: " + color);
    System.out.println("The car Odemeter: " + odometer );
}

or in your test script you could do this.

Scanner keyboard = new Scanner (System.in);

Car a = new Car();
System.out.println("Enter you car Brand Name: ");
a.brandName = keyboard.next();
System.out.println("Enter your car color: ");
a.color = keyboard.next();
System.out.println("Enter your new price: ");
a.priceNew = keyboard.nextDouble();
System.out.println("Enter your Odometer:");
a.updateMilage();
System.out.println();
//UPDATE THE USED PRICE 
a.getPriceAfterUse();

a.outputDetails();
System.out.println();

EDIT #2:

Here is where I think your problem lies.

You have a class Cars, where you have the variables brandName, color, priceNew, priceUsed, and odometer.

I believe you are confusing the return statement of the method with setting the value of the variable.

When you call Car a = new Car(); you have created a new Car Object. Now assume that inside of this car element all of the variables (in this case brandName, color, priceUsed, etc) are empty.

You are setting their values by calling\:

a.brandName = "something"
a.color = "some color"
a.priceNew = "some value"

So now all of these values have been set... except for odometer and priceUsed

you set the odometer with

a.updateMilage();

and then you called:

a.outputDetails(); 

But where along this chain of commands have you changed or even set the value of priceUsed? you have not done this. So when you call outputDetails it will print out all of the values in your Car Object. and since you have not set the value of priceUsed it will print out 0.0

remember that the return in a function does not set the value it just sends a value back to whatever called it... that is how the value gets passed back to System.out.println for example

System.out.println(a.getPriceAfterUse()) will print out the calculated price after use and the value that is printed is that is returned by the public double getPriceAfterUse() method.

BUT the value of the variable priceUsed which is inside your object is SET when you run the line

priceUsed = priceNew*(1-(odometer/6000000));

Until that line us run the value of priceUsed in the Car object will print out 0.0

hmmm... TLDR :)

0

精彩评论

暂无评论...
验证码 换一张
取 消