-4

So basically I am trying to make it interpret if I input more than 200 horsepower, the speed should change increasingly by 15, which works, if I input "BMW" then the speed should go up with 10, which doesn't work and I cant figure out why, and if the horsepower is less than 200 and the brand isn't BMW, it should increase by 5, which also works.

    public string brand;
    public string model;
    public int hp;
    public int speed = 25;

    public int GetSpeed()
    {
        return this.speed;

    }

    public void SetSpeed(int newspeed)
    {
        if (speed < 0 || speed > 260)
        {
            Console.WriteLine("Please input a valid speed");
        }
        else
        {
            this.speed = newspeed;
        }

    }

    public string GetBrand()
    {
        return this.brand;
    }

    public void SetBrand(string newbrand)
    {
        this.brand = newbrand;
    }

    public string GetModel()
    {
        return this.model;
    }

    public void SetModel(string newmodel)
    {
        this.model = newmodel;
    }

    public int GetHp()
    {
        return this.hp;
    }

    public void SetHp(int newhp)
    {
        this.hp = newhp;
    }

    public int Accelerate(int step = 10, int new_speed = 5, int hpov = 15)
    {
        if (hp >= 200)
        {
            this.speed += hpov;
            if (brand == "BMW")
            {
                this.speed += step;
            }
        }
        else if (brand != "BMW")
        {
            this.speed += new_speed;
        }

        return this.speed;
    }

}

}

2 Answers2

3

This is because you only increased the horsepower if you're not BMW instead of increasing it always.

public int Accelerate(int step = 10, int new_speed = 5, int hpov = 15)
{
    if (hp >= 200)
    {
        this.speed += hpov;
        if (brand == "BMW")
        {
            this.speed += step;
        }
    }
    else if (brand != "BMW")
    {
        this.speed += new_speed;
    }

    return this.speed;
}
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
  • I get an endless loop which doesn't change the starting speed -> http://prntscr.com/gs325g This only happens if I input "BMW". – Nick Jmakin Oct 01 '17 at 22:49
0

Aight, "thanks" for the help, remade it to look like this ->

 public int Accelerate(int step = 10, int new_speed = 5, int hpov = 15)
    {
        if (hp >= 200)
        {
            this.speed += hpov;

            if (brand == "BMW")
            {
                this.speed += step;
                return this.speed;
            }
            return this.speed;


        }
        else
        {
            this.speed += new_speed;
            return this.speed;

        }

    }

and it works perfectly as it should.