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.
这篇关于读取双重类型的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:读取双重类型的用户输入
基础教程推荐
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
