简体   繁体   English

我是初学者,编写控制台程序 我正在尝试构建计算器并需要帮助 (C#)

[英]I'm a beginner and write console programs I'm trying to build a calculator and need help (C#)

I want to know how to use a user input similar to Convert.ToInt64(Console.Readline()) but if they type eg: 198.98我想知道如何使用类似于Convert.ToInt64(Console.Readline())的用户输入,但如果他们输入例如:198.98

Into the floating point type you prefer var doesn't work I don't know what else to do进入你喜欢的浮点类型 var 不起作用我不知道还能做什么

you can use this:你可以使用这个:

float.TryParse(Console.Readline());

You should use this method instead of using the Convert class.您应该使用此方法而不是使用 Convert class。 If you want to make sure your program doesn't throw an exception, try float.TryParse() instead.如果您想确保您的程序不会引发异常,请尝试float.TryParse()代替。

var f = float.Parse(Console.ReadLine());

Console.WriteLine(f);
Console.ReadLine();

What does the var part look like? var部分是什么样的?

You should check the input parameter and then try to cast it.您应该检查输入参数,然后尝试强制转换它。 use try parse to cast使用 try parse 进行强制转换

    if(double.TryParse(request.Code, out var doubleNumber))
    {
        throw new ArgumentException();
    }

read this doc: https://docs.microsoft.com/en-us/dotnet/api/system.double.tryparse?view=net-6.0阅读此文档: https://docs.microsoft.com/en-us/dotnet/api/system.double.tryparse?view=net-6.0

Make sure that the user only can input number.确保用户只能输入数字。

var a = float.Parse(Console.ReadLine());

Console.WriteLine(a);

Or或者

double b = Convert.ToDouble(Console.ReadLine());

Console.WriteLine(b);

You can also read this article你也可以阅读这篇文章

https://docs.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/tutorials/numbers-in-csharp-local https://docs.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/tutorials/numbers-in-csharp-local

We have various options for performing this conversion.我们有多种选择来执行这种转换。 The difference between float and double is that float has four places after the decimal point and double has twelve places after the decimal point. float 和 double 的区别在于 float 小数点后有 4 位,double 小数点后有 12 位。 So of you want double you can use it as,所以你想要双倍的你可以用它,

var num = Convert.ToDouble(Console.ReadLine()) var num = Convert.ToDouble(Console.ReadLine())

In case if you want to convert it into float we have two options there, parse and tryparse.如果你想把它转换成浮点数,我们有两个选项,parse 和 tryparse。 Parse returns the converted number and TryParse returns the Boolean value of conversion. Parse 返回转换后的数字,TryParse 返回转换后的 Boolean 值。 You can use it accordingly like,您可以相应地使用它,例如,

var num = float.Parse(Console.ReadLine()) var num = float.Parse(Console.ReadLine())

try this one: for your situation you have to use below code:试试这个:对于您的情况,您必须使用以下代码:

Convert.ToDouble(Console.Readline());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试在 C# 控制台中构建一个简单的计算器 - I'm Trying to Build a Simple Calculator in C# Console C# 数组帮助(我是初学者):) - C# array help (i'm a beginner) :) C#if语句在主机游戏中不起作用(我是初学者,所以可能是完全错误的) - C# if statements not working in console game (I'm a beginner so it may be completely wrong) 我正在尝试让我的记忆功能在C#计算器上工作 - I'm trying to get my memory functions to work on a C# calculator 从C#角度学习UML ......(我是初学者) - Learn UML but from C# perspective… (i'm beginner) 在 C# 中制作我的第一个计算器,我被卡住了 - Making my first calculator in C# and I'm stuck 在寻找我要做的特定事物的名称时需要帮助 - Need help in finding name of specific thing I'm trying to do 我需要一个没有错误的素数计数程序的帮助,但是仍然崩溃(C#) - I'm in need of help for a prime counting program with no error, howerver still crashing (C#) 我收到此错误,如果可能,需要帮助修复它。我正在尝试构建我的项目以查看 output 但它不会让我 - I'm getting this error and need help fixing it if possible.I'm trying to build my project to see the output but it won't let me C#帮助,我是C#的初学者,需要一些编程帮助 - C# Help, I am a beginner in C#, need help in some programming
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM