我一直在使用此代码时出现问题,我尝试寻找答案但找不到任何答案。当我要求用户输入汽车的颜色时,它不会等待输入并立即跳到其他打印语句如何解决这个问题?
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();
}
}
}
答案 0 :(得分:2)
这是因为您在ReadLine之前有一个Read。 Console.Read只读取键盘缓冲区中的第一个字符并返回,但是您输入了数字并按下了Enter键,因此当代码到达ReadLine时,键盘缓冲区中的换行就绪,这正是ReadLine等待的内容。返回
您还应该为NumOfWheels
使用ReadLineConsole.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来检查输入是否是有效数字,并在出现错误时中止程序。 (或者引入循环以继续询问有效输入)
答案 1 :(得分:0)
有时您使用 Console.Read ,有时则使用 Console.ReadLine 。
如果您使用Console.Read,则只读取一个字符,因此当用户输入4并输入时,我们有两个字符&#39; 4&#39;和&#39; \ n&#39;。那&#39; \ n&#39;保存在缓冲区中,下一个Console.read或Console.readline接受它。
您应该只在程序中使用 Console.ReadLine 。