读取双类型的用户输入 [英] read user input of double type

查看:104
本文介绍了读取双类型的用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这在其他地方使用循环回答,但我不知道,如果有一个函数,我不会发现,使这更容易,或者如果这是一个可能的(在我看来)负面到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#.

我想从用户输入中读取一个double,如下所示:

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.

这篇关于读取双类型的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆