C# 输入字符串的格式不正确| | ConsoleApp

C# 输入字符串的格式不正确| | ConsoleApp,c#,console,C#,Console,基本上,我试图不让用户输入字符串而不是整数;但在代码行: else if (Convert.ToString(result) == "") 我犯了一个错误 完整代码: class Program { static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Red; int calcKelvin = 273; int calcFahren =

基本上,我试图不让用户输入字符串而不是整数;但在代码行:

else if (Convert.ToString(result) == "")
我犯了一个错误

完整代码:

class Program
{
    static void Main(string[] args)
    {
        Console.ForegroundColor = ConsoleColor.Red;

        int calcKelvin = 273;
        int calcFahren = 32; 
        int result = Convert.ToInt32(Console.ReadLine());

        if (result == 0)
        {
            Console.WriteLine("Check it up on google!");
            Console.Title = "I'M USELESS CONSOLE, YOU CAN NOW EXIT || I'M USELESS CONSOLE, YOU CAN NOW EXIT || I'M USELESS CONSOLE, YOU CAN NOW EXIT ||";
        }
        else if (Convert.ToString(result) == "")
        {
            Console.Write("Error, you can not convert a text");
        }
        else 
        { 
            Console.WriteLine("Kelvin = " + calcKelvin * result);
            Console.WriteLine("Fahrenheit = " + calcFahren * result);
        }
    }
}

Convert.ToInt32
如果输入字符串不是数字,则引发异常。要解决这个问题,可以改用
int.TryParse

例如:

using System;
class Program
{
static void Main(string[] args)
{

Console.ForegroundColor = ConsoleColor.Red;


int calcKelvin = 273;
int calcFahren = 32; 
int result;
bool isNum=int.TryParse(Console.ReadLine(),out result);

if (!isNum)
{
    Console.Write("Error, you can not convert a text");
}
else if (result == 0)
{
    Console.WriteLine("Check it up on google!");
    Console.Title = "I'M USELESS CONSOLE, YOU CAN NOW EXIT || I'M USELESS CONSOLE, YOU CAN NOW EXIT || I'M USELESS CONSOLE, YOU CAN NOW EXIT ||";
}
else { 

Console.WriteLine("Kelvin = " + calcKelvin * result);
Console.WriteLine("Fahrenheit = " + calcFahren * result);
}
}
}

从字符串中获取数字的最安全方法是使用
TryParse
方法,因为该方法返回两个值!实际返回类型是一个
bool
,它指示字符串是否已成功转换,另一个是一个
out
参数,它属于我们要转换的类型,并设置为转换后的值(如果转换失败,则设置为该类型的默认值)

对于温度,我们通常处理十进制数,因此
double
可能是存储结果的好类型。因此,我们将使用
double.TryParse

现在,由于如果用户出错,我们不一定要退出,我们可能应该在循环中进行转换,因此如果转换失败,我们只要求用户再试一次。由于这段代码也将在其他地方使用,我们可以创建一个helper方法,该方法接收我们向用户显示的提示,并返回强类型用户响应:

private static double GetDoubleFromUser(string prompt = null)
{
    double result;

    do
    {
        Console.Write(prompt);
    } while (!double.TryParse(Console.ReadLine(), out result));

    return result;
}
使用此方法,我们现在只需声明一个double并将其分配给上述方法的返回值,如:

double userInput = GetDoubleFromUser("Enter a temperature: ");
我们可以在代码中更正的另一件事是用于进行转换的公式。在线快速检查显示,我们为开尔文加一个数字,为华氏度做乘法、除法和加法。一旦用户提供了摄氏温度,我们就可以动态计算这些值:

private static void Main()
{
    double celcius = GetDoubleFromUser("Enter a Celcius temperature: ");

    double fahrenheit = celcius * 9 / 5 + 32;
    double kelvin = celcius + 273.15;

    Console.WriteLine("Kelvin = " + kelvin);
    Console.WriteLine("Fahrenheit = " + fahrenheit);

    GetKeyFromUser("Done! Press any key to exit...");
}
输出


我对你的温度计算很感兴趣。除非您的用户输入“-40”,否则您无法简单地通过加法将整数转换为开尔文和华氏度。假设用户输入的值以摄氏度为单位,则可以添加273.15并获得以开尔文为单位的温度。不过,您必须先将设备a摄氏度除以5,然后乘以9,再加上32,得到华氏度。如果您还没有弄明白,那么您还应该将其作为
double
而不是
int
来执行。而且,0摄氏度是一个非常好的温度!可能重复的