如何在C#中打印出来自用户的输入?

时间:2016-02-07 00:36:18

标签: c#

嘿,我几乎没有开始我的C#课程,所以我对编程很新。我无法打印出用户的输入。当我要求用户输入本周运行的里程数时,它将显示输入但是然后打印出0.当它假设打印出“这辆车归”时,我得到一个formatexception是未处理的错误并停止程序。特别是它说Index(从零开始)必须大于或等于零且小于参数列表的大小。我该如何解决这个问题?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1
{
    class Car
    {
        private string color;
        private int numOfWheels;
        private int startingPoint;
        private int mileage;
        private int currentSpeed;

        public Car()
        {
            color = "";
            NumOfWheels = 4;
            StartingPoint = 100000;
            CurrentSpeed = 0;
            Mileage = 0;
        }

        public Car(string color, int numOfWheels, int startingPoint, int currentSpeed, int mileage)
        {
            Color = color;
            NumOfWheels = numOfWheels;
            StartingPoint = startingPoint;
            CurrentSpeed = currentSpeed;
            Mileage = mileage;
        }

        public virtual string Color
        {
            get
            {
                return color;
            }
            set
            {
                color = value;
            }
        }

        public virtual int NumOfWheels
        {
            get
            {
                return numOfWheels;
            }
            set
            {
                numOfWheels = value;
            }
        }

        public virtual int StartingPoint
        {
            get
            {
                return startingPoint;
            }
            set
            {
                startingPoint = value;
            }
        }

        public virtual int CurrentSpeed
        {
            get
            {
                return currentSpeed;
            }
            set
            {
                currentSpeed = value;
            }
        }

        public virtual int Mileage
        {
            get
            {
                return mileage;
            }
            set
            {
                mileage = value;
            }
        }


        public override string ToString()
        {
            return (" color " + color + " numOfWheels" + numOfWheels + "startingPoint " + startingPoint + "mileage" + mileage + "current speed" + currentSpeed);
        }
    }
}
********************************************************************************
///this is the test case


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework1
{
    class CarTest
    {
        static void Main(string[] args)
        {


            Car myCar = new Car();


            Console.WriteLine("*****************************");
            Console.WriteLine("*                           *");
            Console.WriteLine("*  WELCOME TO CAR MANAGER   *");
            Console.WriteLine("*    By <<my Name>>         *");
            Console.WriteLine("*                           *");
            Console.WriteLine("*****************************");



            Console.WriteLine("\nEnter the number of wheels of a car");
            int numOfWheels = Console.Read();
            Console.WriteLine(myCar.NumOfWheels);



            Console.WriteLine("Enter the color of the car");
            String color = Console.ReadLine();
            Console.WriteLine(myCar.Color);

            Console.WriteLine("Current mileage will be set to zero");

            Console.WriteLine("The current starting point will be set to 100000");

            Console.Write("The current status of your car \n{0:D} Wheels, \n{1}, \n{2:D} Miles and \nCAR POINT = {3:D}", myCar.NumOfWheels,
            myCar.Color, myCar.Mileage, myCar.StartingPoint);

            Console.WriteLine("\nEnter the owner's name");
            String name = Console.ReadLine();

            Console.WriteLine("Enter the miles the car ran in this week");
            int mileage = Console.Read();
            Console.WriteLine(myCar.Mileage);

            Console.WriteLine("This car is owned by n{1}", name);
            ///this is where the program stops and i get the error message


            Console.WriteLine("===>The current status of your car:");
            Console.WriteLine("Wheels: " + myCar.NumOfWheels);
            Console.WriteLine("Color: " + myCar.Color);
            Console.WriteLine("Current Mileage: " + myCar.Mileage);
            Console.WriteLine("Starting Point: " + myCar.StartingPoint);
            Console.WriteLine("************ Thank you for using CAR MANAGER *************");
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("Press ENTER to close console…….");
        }
    }
}

1 个答案:

答案 0 :(得分:4)

您的所有代码都有同样的错误:

Console.WriteLine("\nEnter the number of wheels of a car");
int numOfWheels = Console.Read();
Console.WriteLine(myCar.NumOfWheels);

您将值存储在时间变量中,但是您没有将其分配给myCar实例,那么myCar。(无论如何)将始终为null。

将它分配给类属性,它应该可以工作:

Console.WriteLine("\nEnter the number of wheels of a car");
myCar.NumOfWheels = Console.Read();
Console.WriteLine(myCar.NumOfWheels);