Elon's Toy Why the distance don't update

Hello, and here is my code

using System;

class RemoteControlCar
{
    private int distance = 0;
    private int battery = 100;
    public static RemoteControlCar Buy()
    {
      return new RemoteControlCar();
    }

    public string DistanceDisplay()
    {
        return "Driven " + distance + " meters";
    }

    public string BatteryDisplay()
    {
        if (distance == 2000)
        {
            return "Battery empty";
        }
        else
        {
            return "Battery at "+battery+"%";
        }
    }

    public void Drive()
    {
        if(battery > 0)
        {
            distance = + 20;
            battery = - 1;
            
        }
        
    }
}

when it run


var car = new RemoteControlCar();
for (var i = 0; i < 17; i++)
        {
            car.Drive();
        }

it reports

Assert.Equal() Failure: Strings differ
↓ (pos 7)
Expected: “Driven 340 meters”
Actual: “Driven 20 meters”
↑ (pos 7)

it’s seems like my code failure to update the value of distance.

What is the value of the battery after the first call? What does the second call do with the battery check?

Take a close look at this: is it doing what you want it to do?

distance = + 20;
1 Like

Thank you!!