我想询问用户他/她的体重;例如;
Console.Write("Please enter your weight: {I want it to be inserted here} kg");
这里的输入将在 kg 之后插入,尽管我希望它就在“kg”之前,以便用户可以知道预期的 kg 值,并且不需要进一步的细节(例如 kg/lbs.. )
提前致谢。
我想询问用户他/她的体重;例如;
Console.Write("Please enter your weight: {I want it to be inserted here} kg");
这里的输入将在 kg 之后插入,尽管我希望它就在“kg”之前,以便用户可以知道预期的 kg 值,并且不需要进一步的细节(例如 kg/lbs.. )
提前致谢。
您必须阅读用户输入的每一个键,然后将光标位置设置为您希望显示用户体重的位置。然后你把它写到控制台。
static void Main(string[] args)
{
string prompt = "Please enter your weight: ";
Console.Write(prompt + " kg");
ConsoleKeyInfo keyInfo;
string weightInput = string.Empty;
while ((keyInfo = Console.ReadKey()).Key != ConsoleKey.Enter)
{
//set position of the cursor to the point where the user inputs wight
Console.SetCursorPosition(prompt.Length, 0);
//if a wrong value is entered the user can remove it
if (keyInfo.Key.Equals(ConsoleKey.Backspace) && weightInput.Length > 0)
{
weightInput = weightInput.Substring(0, weightInput.Length - 1);
}
else
{
//append typed char to the input before writing it
weightInput += keyInfo.KeyChar.ToString();
}
Console.Write(weightInput + " kg ");
}
//process weightInput here
}
我找到了一个简单的答案:
Console.Write("enter weight = kg");
Console.SetCursorPosition(0, 8);
metric.weightKgs = byte.Parse(Console.ReadLine());
这一切都归结为光标定位和玩弄它找到确切的位置。
您需要将您的问题写到控制台(我会这样做)
Console.WriteLine("Please enter your weight (kg):");
然后等待值回来。
这将等待用户
string userInput = Console.ReadLine();
在字符串上使用美元符号允许字符串插值。根据您的 C# 版本,这可能不起作用。
Console.WriteLine($"Your weight is {userInput}kg.");
int weight = Console.ReadLine();
if (weight != null)
Console.WriteLine(string.format("Please enter your weight: {1} kg", weight));
这是未经检查的代码。但应该是这样的。