问题描述
我发现在其他地方使用循环回答了这个问题,但我不确定是否真的有一个我没有找到的函数可以让这更容易,或者这是否可能(在我看来)消极的一面到 C#.
I have found this answered in other places using loops, but I wasn't sure if there is actually a function that I'm not finding that makes this easier, or if this is a possible (in my opinion) negative side to C#.
我正在尝试从这样的用户输入中读取双精度:
I'm trying to read in a double from user input like this:
Console.WriteLine("Please input your total salary: ") // i input 100
double totalSalary = Console.Read(); //reads in the 1, changes to 49.
我在这方面找到了其他几个帖子,他们都有不同的答案,而且提出的问题也不完全相同.如果我只想读取用户输入,那么最好的方法是什么?
I've found a couple other posts on this, and they all have different answers, and the questions asked aren't exactly the same either. If i just want the user input read in, what is the best way to do that?
推荐答案
你必须检查整个事情.因为 Console.Read()
返回一个整数.
You'll have to check the entire thing on it's way in.. as Console.Read()
returns an integer.
double totalSalary;
if (!double.TryParse(Console.ReadLine(), out totalSalary)) {
// .. error with input
}
// .. totalSalary is okay here.
这篇关于读取双重类型的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!