在C#中接受小数作为输入时需要帮助 [英] Need help with accepting decimals as input in C#
问题描述
我已经用C#编写了一个程序,该程序运行勾股定理.我希望在允许程序接受用户输入的小数点方面有所帮助.这就是我所拥有的.
I have written a program in C# that runs the Pythagorean Theorem. I would love some help on allowing the program to accept decimal points from the user input. This is what I have.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Project_2
{
class Program
{
static void Main(string[] args)
{
int sideA = 0;
int sideB = 0;
double sideC = 0;
Console.Write("Enter a integer for Side A ");
sideA = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter a integer for Side B ");
sideB = Convert.ToInt16(Console.ReadLine());
sideC = Math.Pow((sideA * sideA + sideB * sideB), .5);
Console.Write("Side C has this length...");
Console.WriteLine(sideC);
Console.ReadLine();
}
}
}
我一直在尝试使用Math.Abs等进行研究,以仅接收构建错误.在写路径方面的帮助将不胜感激.
I have been trying to research this by using the Math.Abs and so on only to receive build errors. Help in the write path would be greatly appreciated.
推荐答案
我建议使用Decimal.TryParse.这种模式非常安全,因为它会捕获异常并返回一个布尔值,以确定解析操作是否成功.
I would recommend using Decimal.TryParse. This pattern is very safe since it traps the exceptions and returns a boolean to determine the success of the parse operation.
http://msdn.microsoft.com/zh-我们/library/system.decimal.tryparse.aspx
这篇关于在C#中接受小数作为输入时需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!