C# 如何在同一行中获取用户输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12556279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to take user input in the same line?
提问by user1692696
I'm bigener in C# programming So, I was just wondering about how to take user input in the same line? this is my code and also I want to print the output in the same line
我在 C# 编程方面比较厉害 所以,我只是想知道如何在同一行中获取用户输入?这是我的代码,我也想在同一行打印输出
using System;
namespace Wa2
{
class BodyMassCalculation
{
public static void Main (string[] args)
{
Console.WriteLine ("BMI Calculator\n");
double weight;
Console.WriteLine ("Enter your weight in kilograms: ");
weight = Convert.ToInt16(Console.ReadLine());
double height;
Console.WriteLine ("Enter your height in centimetres: ");
height = Convert.ToInt16(Console.ReadLine());
double meter;
meter = height / 100;
Double BMI;
BMI = (weight) / (meter*meter);
Console.WriteLine ("Your BMI is " , BMI);
Console.WriteLine(BMI.ToString("00.00"));
}
}
}
采纳答案by matthewr
Try this:
尝试这个:
Console.Write("Enter your input here: ");
string userinput = Console.ReadLine();
Just change Console.WriteLineto Console.Write.
只需更改Console.WriteLine为Console.Write.
回答by Mister Bee
Use Console.Write()instead of Console.WriteLine().
使用Console.Write()代替Console.WriteLine()。
I think that's what you mean anyway, the question isn't very clear.
我认为这就是你的意思,问题不是很清楚。
回答by paulsm4
I think you're asking if it's possible to read both height and weight at the same time:
我想您是在问是否可以同时读取身高和体重:
// C equivalent
printf ("Enter height (cm) and weight (kg): ");
scanf ("%d %d\n", &h, &w);
Yes, there are several alternatives.
是的,有几种选择。
Arguably the easiest is use Console.ReadLine() (like you're doing) and parse the string.
可以说最简单的是使用 Console.ReadLine() (就像你正在做的那样)并解析字符串。
You can also try multiple "Console.Read()" (one for each argument).
您还可以尝试多个“Console.Read()”(每个参数一个)。

