简体   繁体   中英

How to clear CS0029 C# Cannot implicitly convert type 'void' to 'int' and CS1729 C# 'Car' does not contain a constructor that takes 4 arguments

I cant figure out what to change. I have played with the void and int.

The problem is with the SlowDown and SpeedUp methods, and in the lines Car car1 = new Car("Ford", "Focus", 2010, car1Speed); The CS1729 is flagging the Car before the ("Ford", "Focus", 2010, car1Speed) .

How do I fix these errors?

class Car
{
    private int Speed;
    private string Make;
    private string Model;
    private int Year;

    public void Car1(string make, string model, int year, int speed)
    {
        this.Make = make;
        this.Model = model;
        this.Year = year;
        this.Speed = speed;
    }
    public void Car2(string make, string model, int year, int speed)
    {
        this.Make = make;
        this.Model = model;
        this.Year = year;
        this.Speed = 0;
    }
    public void SpeedUp()
    {
        Speed = Speed ++;
    }


    public void SlowDown()
    {
        Speed = Speed --;
    }
    public void Display()
    {
        Console.WriteLine(Year + " " + Make + " " + Model + " is going " + Speed + " MPH.");
    }

    public static void Main(string[] args)
    {
        int car1Speed = 20;
        int car2Speed = 0;
        Car car1 = new Car("Ford", "Focus", 2010, car1Speed);
        Car car2 = new Car("Chevy", "Cruze", 2018, car2Speed);

        for (int i = 0; i < 60; i++)
        {
            if (i % 2 == 0)
            {
                car2Speed = car2.SpeedUp();
            }
            if (i % 3 == 0)
            {
                car1Speed = car1.SpeedUp();
            }
            if (i % 5 == 0)
            {
                car1Speed = car1.SlowDown();
                car2Speed = car2.SlowDown();
            }
        }
        car1.Display();
        car2.Display();

    }
}

}

From what I gathered in your comments it seems this is one of those bad assignments where they teach you bad coding practices by providing poorly written code. This is disastrous practice that poisons the industry because bad habits don't come off easily. Here is the full code how it should be written and comments why it should be that way.

public class Car
{
    // Member fields should be either "_camelCase" or "camelCase"
    private string _make;
    private string _model;
    private int _year;
    private int _speed;

    // Don't duplicate the code, default value for speed makes it optional parameter. 
    // There is no need for a second constructor.
    public Car(string make, string model, int year, int speed = 0)
    {
        _make = make;
        _model = model;
        _year = year;
        _speed = speed;
    }

    public int SpeedUp()
    {
        // Increments speed and returns the value.
        // Might as well be void without return since example doesn't use it anywhere.
        return _speed++;
    }

    public int SlowDown()
    {
        // If speed is more than 1, decrement and then return the value
        return _speed > 0 ? _speed-- : 0;
    }

    public void Display()
    {
        // String interpolations are much easier to read
        Console.WriteLine($"{_year} {_make} {_model} is going {_speed} MPH.");
    }
}

public class Program
{
    // Main is entry point for your app, it doesn't belong in any type class like Car, 
    // because it's not relevant to it
    private static void Main()
    {
        // Use descriptive names for variables, you already know one car is Ford and the 
        // other is Chevy so name them as such so you know which is which
        // later in the code. 
        var fordCar = new Car("Ford", "Focus", 2010, 20);
        var chevyCar = new Car("Chevy", "Cruze", 2018); // No speed parameter

        // Ommited variables car1Speed, car2Speed. You shouldn't declare variables 
        // that serve no purpose and aren't even in use.

        for (int i = 0; i < 60; i++)
        {
            if (i % 2 == 0)
            {
                // Returns int, but variable assignment ins't necessary to anything here
                chevyCar.SpeedUp(); 
            }
            if (i % 3 == 0)
            {
                fordCar.SpeedUp();
            }
            if (i % 5 == 0)
            {
                fordCar.SlowDown();
                chevyCar.SlowDown();
            }
        }

        fordCar.Display();
        chevyCar.Display();
    }
}

You have misunderstood the term constructor. You don't need a constructor for every object you create. Just make a constructor named Car and your code should work.

As an advice, place the main function inside an another class. The main function has nothing to do with a car.

Also speed = speed++ or speed = speed-- is bad writing , plain speed++ or speed-- is preferred.

Sounds like all you need is a single constructor with an optional parameter

class Car
{

    public void Car(string make, string model, int year, int speed = 0)
    {
        this.Make = make;
        this.Model = model;
        this.Year = year;
        this.Speed = speed;
    }

    ...

Additional Resources

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM