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.
我想从用户那里得到一个数字,然后用这个数字乘以。我的尝试如下。而是一个包含胡言乱语。例如,如果我插入22,那么a包含50。我做错了什么?我没有得到任何编译错误。
double a,b;a = Console.Read();b = a * Math.PI;Console.WriteLine(b);
8 个解决方案
#1
47
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.
这将只从控制台读取一个字符。
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.}
#2
8
a = double.Parse(Console.ReadLine());
Beware that if the user enters something that cannot be parsed to a double, an exception will be thrown.
请注意,如果用户输入的内容不能被解析为double,则会抛出异常。
Edit:
编辑:
To expand on my answer, the reason it's not working for you is that you are getting an input from the user in string format, and trying to put it directly into a double. You can't do that. You have to extract the double value from the string first.
为了扩展我的答案,它不适合你的原因是你从用户那里获得了一个字符串格式的输入,并试图把它直接放到一个double中。你不能这样做。您必须首先从字符串中提取double值。
If you'd like to perform some sort of error checking, simply do this:
如果您想执行某种错误检查,只需执行以下操作:
if ( double.TryParse(Console.ReadLine(), out a) ) { Console.Writeline("Sonuç "+ a * Math.PI;); }else { Console.WriteLine("Invalid number entered. Please enter number in format: #.#");}
Thanks to Öyvind and abatischev for helping me refine my answer.
感谢Oyvind和abatischev帮助我完善我的答案。
#3
3
string input = Console.ReadLine();double d;if (!Double.TryParse(input, out d)) Console.WriteLine("Wrong input");double r = d * Math.Pi;Console.WriteLine(r);
The main reason of different input/output you're facing is that Console.Read()
returns char code, not a number you typed! Learn how to use MSDN.
您所面对的不同输入/输出的主要原因是Console.Read()返回char代码,而不是您输入的数字!学习如何使用MSDN。
#4
2
I think there are some compiler errors.
我认为有一些编译错误。
- Writeline should be WriteLine (capital 'L')
- 写作应该是写作(大写的“L”)
-
missing semicolon at the end of a line
在一行末尾缺少分号
double a, b; Console.WriteLine("istenen sayıyı sonuna .00 koyarak yaz"); a = double.Parse(Console.ReadLine()); b = a * Math.PI; // Missing colon! Console.WriteLine("Sonuç " + b);
#5
1
Sometime in the future .NET4.6
将来的某个时候。net4.6
//for Doubledouble inputValues = double.Parse(Console.ReadLine());//for Intint inputValues = int.Parse(Console.ReadLine());
#6
0
You're missing a semicolon: double b = a * Math.PI;
您缺少一个分号:double b = a * Math.PI;
#7
0
double a,b; Console.WriteLine("istenen sayıyı sonuna .00 koyarak yaz"); try { a = Convert.ToDouble(Console.ReadLine()); b = a * Math.PI; Console.WriteLine("Sonuç " + b); } catch (Exception) { Console.WriteLine("dönüştürme hatası"); throw; }
#8
0
string str = Console.ReadLine(); //Reads a character from consoledouble a = double.Parse(str); //Converts str into the type doubledouble b = a * Math.PI; // Multiplies by PIConsole.WriteLine("{0}", b); // Writes the number to console
Console.Read()
reads a string from console A SINGLE CHARACTER AT A TIME (but waits for an enter before going on. You normally use it in a while
cycle). So if you write 25
+ Enter, it will return the unicode value of 2
that is 50
. If you redo a second Console.Read()
it will return immediately with 53
(the unicode value of 5
). A third and a fourth Console.Read()
will return the end of line/carriage characters. A fifth will wait for new input.
read()每次从控制台读取单个字符的字符串(但在继续之前等待输入)。你通常会在一段时间内使用它)。如果你写25 + Enter,它会返回2的unicode值,也就是50。如果重做第二个Console.Read(),它将立即返回53 (unicode值为5)。五分之一将等待新的输入。
Console.ReadLine()
reads a string
(so then you need to change the string to a double
)
readline()读取字符串(因此需要将字符串更改为double)
#1
47
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.
这将只从控制台读取一个字符。
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.}
#2
8
a = double.Parse(Console.ReadLine());
Beware that if the user enters something that cannot be parsed to a double, an exception will be thrown.
请注意,如果用户输入的内容不能被解析为double,则会抛出异常。
Edit:
编辑:
To expand on my answer, the reason it's not working for you is that you are getting an input from the user in string format, and trying to put it directly into a double. You can't do that. You have to extract the double value from the string first.
为了扩展我的答案,它不适合你的原因是你从用户那里获得了一个字符串格式的输入,并试图把它直接放到一个double中。你不能这样做。您必须首先从字符串中提取double值。
If you'd like to perform some sort of error checking, simply do this:
如果您想执行某种错误检查,只需执行以下操作:
if ( double.TryParse(Console.ReadLine(), out a) ) { Console.Writeline("Sonuç "+ a * Math.PI;); }else { Console.WriteLine("Invalid number entered. Please enter number in format: #.#");}
Thanks to Öyvind and abatischev for helping me refine my answer.
感谢Oyvind和abatischev帮助我完善我的答案。
#3
3
string input = Console.ReadLine();double d;if (!Double.TryParse(input, out d)) Console.WriteLine("Wrong input");double r = d * Math.Pi;Console.WriteLine(r);
The main reason of different input/output you're facing is that Console.Read()
returns char code, not a number you typed! Learn how to use MSDN.
您所面对的不同输入/输出的主要原因是Console.Read()返回char代码,而不是您输入的数字!学习如何使用MSDN。
#4
2
I think there are some compiler errors.
我认为有一些编译错误。
- Writeline should be WriteLine (capital 'L')
- 写作应该是写作(大写的“L”)
-
missing semicolon at the end of a line
在一行末尾缺少分号
double a, b; Console.WriteLine("istenen sayıyı sonuna .00 koyarak yaz"); a = double.Parse(Console.ReadLine()); b = a * Math.PI; // Missing colon! Console.WriteLine("Sonuç " + b);
#5
1
Sometime in the future .NET4.6
将来的某个时候。net4.6
//for Doubledouble inputValues = double.Parse(Console.ReadLine());//for Intint inputValues = int.Parse(Console.ReadLine());
#6
0
You're missing a semicolon: double b = a * Math.PI;
您缺少一个分号:double b = a * Math.PI;
#7
0
double a,b; Console.WriteLine("istenen sayıyı sonuna .00 koyarak yaz"); try { a = Convert.ToDouble(Console.ReadLine()); b = a * Math.PI; Console.WriteLine("Sonuç " + b); } catch (Exception) { Console.WriteLine("dönüştürme hatası"); throw; }
#8
0
string str = Console.ReadLine(); //Reads a character from consoledouble a = double.Parse(str); //Converts str into the type doubledouble b = a * Math.PI; // Multiplies by PIConsole.WriteLine("{0}", b); // Writes the number to console
Console.Read()
reads a string from console A SINGLE CHARACTER AT A TIME (but waits for an enter before going on. You normally use it in a while
cycle). So if you write 25
+ Enter, it will return the unicode value of 2
that is 50
. If you redo a second Console.Read()
it will return immediately with 53
(the unicode value of 5
). A third and a fourth Console.Read()
will return the end of line/carriage characters. A fifth will wait for new input.
read()每次从控制台读取单个字符的字符串(但在继续之前等待输入)。你通常会在一段时间内使用它)。如果你写25 + Enter,它会返回2的unicode值,也就是50。如果重做第二个Console.Read(),它将立即返回53 (unicode值为5)。五分之一将等待新的输入。
Console.ReadLine()
reads a string
(so then you need to change the string to a double
)
readline()读取字符串(因此需要将字符串更改为double)