如何编程一个错误信息,当用户输入一个十进制分数到一个双变量与点而不是逗号在c# ?



亲爱的业余和资深开发者,

我是一个初学者,最近开始更认真地学习c#(我已经在高中时接触了c#的世界,但现在,我更认真地学习它)。我做了一个简单的命令提示符应用程序,作为我大学高级编程语言课的作业:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_002_01
{
class Program
{
static void Main(string[] args)
{

Console.Write("Please enter the length of the triangle's a side (cm)! ");
double a;
bool a_if = double.TryParse(Console.ReadLine(), out a);
Console.Write("Please enter the length of the triangle's b side (cm)! ");
double b;
bool b_if = double.TryParse(Console.ReadLine(), out b);
Console.Write("Please enter the length of the triangle's c side (cm)! ");
double c;
bool c_if = double.TryParse(Console.ReadLine(), out c);
if (a_if == true && a > 0 && b > 0 && c > 0)
{
double res1 = a + b;
if (res1 > c)
{
Console.WriteLine("Triangle equality is present.");
}
else
{
Console.WriteLine("Triangle equality is not present.");
}
}
else if (b_if == true && a > 0 && b > 0 && c > 0)
{
double res2 = a + b;
if (res2 > c)
{
Console.WriteLine("Triangle equality is present.");
}
else
{
Console.WriteLine("Triangle equality is not present.");
}
}
else if (c_if == true && a > 0 && b > 0 && c > 0)
{
double ossz3 = a + b;
if (res3 > c)
{
Console.WriteLine("Triangle equality is present.");
}
else
{
Console.WriteLine("Triangle equality is not present.");
}
}
else
{
Console.WriteLine("You cannot make a tringle with these numbers.");
}
Console.ReadKey();
}
}
}

这个小程序用用户输入的数字检查三角形中的三角形是否相等。它运行得很好,但我只是不知道如何制造一个错误信息,说明程序不能理解用点代替逗号的十进制分数。这将是对我的程序的一个很好的修饰,以原谅我的初学者级别的面条式代码。

我希望有人尽快回复。

double.TryParse已经通过使用默认区域性检查了数字格式。因此,如果您的区域性数字格式中不存在点,并且用户输入它而不是逗号,则double.TryParse返回false。

我已经写了一个方法,将帮助您确保有一个逗号,它取决于读取和操作您所做的读取,然后转换为双精度。之前你的阅读行是:

bool a_if = double.TryParse(Console.ReadLine(), out a);

现在:

bool a_if = double.TryParse(isWithComma(Console.ReadLine()), out a);

实际上,该方法将检查逗号是否在数字内,如果显示错误消息,程序将停止,并且我已经添加了红色以在错误中给出更多的表达,同时它返回一个字符串,如果数字是正确的并且不包含逗号,那么程序将没有问题地完成

这是修改后的整个代码,有一个变量(ossz3)显示了一个错误,我已经修改了,请检查:

using System;
namespace HW_002_01
{
class Program
{
//New by: https://github.com/MohammadYAmmar
public static string isWithComma(string value)
{
if (value.Contains(","))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error: cannot be used comma instead of a dot.");
Console.WriteLine("Please try again using the point  (dot) for example 3.3");
System.Environment.Exit(0);
return value;//Won't happen
}
else
{
return value;
}
}
static void Main(string[] args)
{
Console.Write("Please enter the length of the triangle's a side (cm)! ");
double a;
bool a_if = double.TryParse(isWithComma(Console.ReadLine()), out a);
Console.Write("Please enter the length of the triangle's b side (cm)! ");
double b;
bool b_if = double.TryParse(isWithComma(Console.ReadLine()), out b);
Console.Write("Please enter the length of the triangle's c side (cm)! ");
double c;
bool c_if = double.TryParse(isWithComma(Console.ReadLine()), out c);
if (a_if == true && a > 0 && b > 0 && c > 0)
{
double res1 = a + b;
if (res1 > c)
{
Console.WriteLine("Triangle equality is present.");
}
else
{
Console.WriteLine("Triangle equality is not present.");
}
}
else if (b_if == true && a > 0 && b > 0 && c > 0)
{
double res2 = a + b;
if (res2 > c)
{
Console.WriteLine("Triangle equality is present.");
}
else
{
Console.WriteLine("Triangle equality is not present.");
}
}
else if (c_if == true && a > 0 && b > 0 && c > 0)
{
//Old
//double ossz3 = a + b;
//New: Please check that the spelling is correct so that the code is correct for this variable as you wanted it to
double res3 = a + b;
if (res3 > c)
{
Console.WriteLine("Triangle equality is present.");
}
else
{
Console.WriteLine("Triangle equality is not present.");
}
}
else
{
Console.WriteLine("You cannot make a tringle with these numbers.");
}
Console.ReadKey();

}
}
}

祝你好运,并有一个良好的旅程学习c#:)

最新更新