读取双重类型的用户输入

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

问题描述

我发现在其他地方使用循环回答了这个问题,但我不确定是否真的有一个我没有找到的函数可以让这更容易,或者这是否可能(在我看来)消极的一面到 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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)