【问题标题】:How do i ask for user input in C#? [duplicate]如何在 C# 中要求用户输入? [复制]
【发布时间】:2016-02-07 18:04:41
【问题描述】:

我在这段代码中遇到了一段时间的问题,我尝试寻找答案,但找不到任何答案。当我要求用户输入汽车的颜色时,它不会等待输入并立即跳到另一个打印语句我该如何解决这个问题?

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 = 0;
            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);
        }
    }
}


********************************************************************************

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");
            myCar.NumOfWheels = Console.Read();


            Console.WriteLine("Enter the color of the car");
            myCar.Color = Console.ReadLine();
            ///this line does not for wait input. it prints out but          immediately continues to the other print statements


            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 {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");
            myCar.Mileage = Console.Read();


            Console.WriteLine("This car is owned by {0}", name);


            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…….");
            Console.ReadKey(); 

        }
    }
}

【问题讨论】:

  • 如果你想读取一个数字 - 使用 Console.ReadLine() 然后将其解析为所需的类型(int、long、double 等)。您可以使用int.Parse()long.Parse() 等。
  • 在同一程序上的(现在是 4 个)问题之后,每次唯一的改进是直接回答您当时的问题,我想知道您做了多少?我几乎可以肯定,让您遇到的每一个障碍都由互联网上的某个人来解决,这不是您的教授的意图。

标签: c#


【解决方案1】:

发生这种情况是因为您在该 ReadLine 之前有一个 Read。 Console.Read 仅从键盘缓冲区读取第一个字符并返回,但是您已经输入了数字并按下了 Enter,因此当代码到达 ReadLine 时,键盘缓冲区中准备好换行符,这正是 ReadLine 等待返回的

您也应该为 NumOfWheels 使用 ReadLine

Console.WriteLine("\nEnter the number of wheels of a car");
string userInput = Console.ReadLine();
int numOfWheels;
if(Int32.TryParse(userInput, out numOfWheels))
    myCar.NumOfWheels = numOfWheels;
else
{
   Console.WriteLine("You haven't typed a valid number for wheels!")
   return;
}


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

但这会带来一个问题。 NumOfWheels 属性是一个整数,因此您不能直接将返回的字符串分配给该属性。在这里,您需要使用 Int32.TryParse 检查输入是否为有效数字,并在出现错误时中止程序。 (或引入循环以继续询问有效输入)

【讨论】:

  • 这很有效,谢谢。所以 Console.ReadLine() 正在跳过,因为按 Enter 实际上是用户输入?
  • 嗯,从技术上讲,是在键盘缓冲区中插入的一系列数值。为简单起见,假设字符为单个字节,您键入 4 并按 Enter。在键盘缓冲区中,您将找到 52(4 的 ASCII 代码),然后是 13,然后是 10(Windows 换行符)。第一个值由 read 返回,而 13 和 10 保留在缓冲区中以供后面的 ReadLine 使用,后者期望这些字节作为行终止符,因此它立即返回。有趣的是,Read 返回一个整数,所以你的 Car 将有 52 个轮子。
  • 好的,我明白了,我之前遇到了这个问题,它打印出我有 52 个轮子,但是现在当我输入例如 50 英里时,它说我最终得到了 53 个
  • 同样的问题。 “5”的 ASCII 码是 53,而“0”丢失了,因为您的程序在没有任何其他 Read/ReadLine 的情况下终止。您在这里也需要模式(ReadLine、Int32.TryParse)
  • 是的,我明白了,谢谢我现在明白了很多
【解决方案2】:

有时您使用 Console.Read,有时您使用 Console.ReadLine

如果你使用 Console.Read 你只读取一个字符,因此当用户输入 4 并输入时,我们有两个字符 '4' 和 '\n'。该 '\n' 保存在缓冲区中,下一个 Console.read 或 Console.readline 将使用它。

您应该只在程序中使用 Console.ReadLine

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 2015-06-30
    • 2021-12-28
    • 2020-01-30
    • 2015-03-28
    • 1970-01-01
    • 2017-07-16
    相关资源
    最近更新 更多