如何从控制台读取用户输入? [英] How can I read user input from the console?
本文介绍了如何从控制台读取用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从用户那里得到一个数字,然后将该数字乘以Pi。我对此的尝试如下。但是 a
包含乱码。例如,如果我插入 22
,则 a
包含 50
。我究竟做错了什么?我没有得到任何编译器错误。
I want to get a number from the user, and then multiply that number with Pi. my attempt at this is below. But a
contains gibberish. For example, if I insert 22
, then a
contains 50
. What am I doing wrong? I don't get any compiler errors.
double a,b;
a = Console.Read();
b = a * Math.PI;
Console.WriteLine(b);
推荐答案
我不确定您的问题是什么(因为您尚未告诉我们),但我猜是在
I'm not sure what your problem is (since you haven't told us), but I'm guessing at
a = Console.Read();
这只会从控制台读取一个字符。
This will only read one character from your Console.
您可以将程序更改为此。为了使其更强大,请接受多个char输入,并确认输入实际上是数字:
You can change your program to this. To make it more robust, accept more than 1 char input, and validate that the input is actually a number:
double a, b;
Console.WriteLine("istenen sayıyı sonuna .00 koyarak yaz");
if (double.TryParse(Console.ReadLine(), out a)) {
b = a * Math.PI;
Console.WriteLine("Sonuç " + b);
} else {
//user gave an illegal input. Handle it here.
}
这篇关于如何从控制台读取用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文