Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中打印用户的输入?_C# - Fatal编程技术网

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

如何在C#中打印用户的输入?,c#,C#,嘿,我刚开始上C#类课程,所以我对编程相当陌生。我无法打印用户的输入。当我要求用户输入本周运行的英里数时,它将显示输入,但随后打印出0。另外,当它假设打印出“这辆车归谁所有”时,我得到一个formatexception未经处理的错误并停止程序。具体地说,索引(基于零)必须大于或等于零,并且小于参数列表的大小。我该如何解决这个问题 using System; using System.Collections.Generic; using System.Linq; using System.Text

嘿,我刚开始上C#类课程,所以我对编程相当陌生。我无法打印用户的输入。当我要求用户输入本周运行的英里数时,它将显示输入,但随后打印出0。另外,当它假设打印出“这辆车归谁所有”时,我得到一个formatexception未经处理的错误并停止程序。具体地说,索引(基于零)必须大于或等于零,并且小于参数列表的大小。我该如何解决这个问题

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…….");
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间作业1
{
班车
{
私有字符串颜色;
私家车;
私人int启动点;
私人国际里程;
私家车速度;
公共汽车
{
颜色=”;
NumOfWheels=4;
起始点=100000;
当前速度=0;
里程=0;
}
公共汽车(字符串颜色、整数个车轮、整数个起动点、整数个当前速度、整数个里程)
{
颜色=颜色;
NumOfWheels=NumOfWheels;
起始点=起始点;
当前速度=当前速度;
里程=里程;
}
公共虚拟字符串颜色
{
得到
{
返回颜色;
}
设置
{
颜色=值;
}
}
公共虚拟int NumOfWheels
{
得到
{
返回车轮;
}
设置
{
numOfWheels=值;
}
}
公共虚拟整数起始点
{
得到
{
返回起始点;
}
设置
{
起始点=数值;
}
}
公共虚拟速度
{
得到
{
回流速度;
}
设置
{
当前速度=值;
}
}
公共虚拟里程
{
得到
{
返程里程;
}
设置
{
里程=价值;
}
}
公共重写字符串ToString()
{
返回(“颜色”+“数字车轮”+“数字车轮”+“启动点”+“启动点”+“里程”+“当前速度”+当前速度”);
}
}
}
********************************************************************************
///这是测试用例
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间作业1
{
类卡特斯特
{
静态void Main(字符串[]参数)
{
我的车=新车();
Console.WriteLine(“*************************************”);
控制台。写线(**);
Console.WriteLine(“*欢迎使用汽车管理器*”);
Console.WriteLine(“*By*”);
控制台。写线(**);
Console.WriteLine(“*************************************”);
Console.WriteLine(\n输入汽车的车轮数);
int numOfWheels=Console.Read();
控制台。WriteLine(myCar.NumOfWheels);
Console.WriteLine(“输入汽车的颜色”);
字符串颜色=Console.ReadLine();
控制台。WriteLine(myCar.Color);
Console.WriteLine(“当前里程将设置为零”);
Console.WriteLine(“当前起点将设置为100000”);
Console.Write(“您的汽车\n{0:D}车轮的当前状态,\n{1}、\n{2:D}英里和\nCAR点={3:D}”,myCar.NumOfWheels,
我的车。颜色,我的车。里程,我的车。起点);
Console.WriteLine(\n输入所有者的名称);
字符串名称=Console.ReadLine();
Console.WriteLine(“输入本周汽车行驶的里程”);
int milies=Console.Read();
控制台。写入线(我的车。里程);
Console.WriteLine(“这辆车属于n{1}”,名称);
///这是程序停止的地方,我得到了错误消息
Console.WriteLine(“=>您汽车的当前状态:”);
控制台。WriteLine(“车轮:+myCar.NumOfWheels”);
控制台.WriteLine(“颜色:+myCar.Color”);
Console.WriteLine(“当前里程:+myCar.Miledge”);
Console.WriteLine(“起点:+myCar.StartingPoint”);
Console.WriteLine(“*************感谢您使用汽车管理器*************”);
Console.WriteLine(“------------------------------------------------------------------”;
Console.WriteLine(“------------------------------------------------------------------”;
Console.WriteLine(“按ENTER键关闭控制台……);
}
}
}

您的所有代码中都有相同的错误:

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);

当您上一次在一小时前的问题中发布这个精确的程序时,我向您展示了如何设置属性值。还有人对你现在遇到的这个问题的确切原因发表了评论。。。