C# 当输入某些数据或错误答案时,使程序完全不起作用

C# 当输入某些数据或错误答案时,使程序完全不起作用,c#,console-application,C#,Console Application,我有一个疯狂的想法,我希望一个程序在控制台中输入错误数据时不执行任何操作。比如字母表,奇怪的字符。我想要的只是十进制数字和一个可以接受的句号。如果输入了错误的数据,我希望程序保持在那里,在你点击回车键后什么也不做 我的想法是: if (sum != decimal) { // Don't do anything, just leave it as is. code I have no clue about. } 现在,您一定在想,不能将数据类型用于if语句!也许你可以,但这

我有一个疯狂的想法,我希望一个程序在控制台中输入错误数据时不执行任何操作。比如字母表,奇怪的字符。我想要的只是十进制数字和一个可以接受的句号。如果输入了错误的数据,我希望程序保持在那里,在你点击回车键后什么也不做

我的想法是:

if (sum != decimal)
{
   // Don't do anything, just leave it as is. 
    code I have no clue about. 

}
现在,您一定在想,不能将数据类型用于if语句!也许你可以,但这对我不起作用。对不起,我是个大傻瓜

try
{

    Console.WriteLine("Put in the price of the product");

    string input = Console.ReadLine();
    decimal sum = Convert.ToDecimal(input);

    if (sum <= 100)
    {
        decimal totalprice = sum * .90m;
        Console.WriteLine("Your final price is {0:0:00}", totalprice);

    }

}


catch
{

}
试试看
{
Console.WriteLine(“输入产品价格”);
字符串输入=Console.ReadLine();
十进制和=转换为十进制(输入);

如果(sum数据类型未写入控制台。只能从控制台输入中检索字符串。什么类型的字符串
“2”
-十进制、整数、字节、字符串?您所能做的就是尝试从输入字符串中解析某些类型:

Int32.TryParse("2", out value)
对于您的情况:

Console.WriteLine("Put in the price of the product");
string input = Console.ReadLine();
decimal sum;
if (!Decimal.TryParse(input, out sum))
{
    Console.WriteLine("Decimal number cannot be parsed from your input.");
    return;
}

if (sum <= 100)
    Console.WriteLine("Your final price is {0:0:00}", sum * 0.90M);
Console.WriteLine(“输入产品价格”);
字符串输入=Console.ReadLine();
十进制和;
if(!Decimal.TryParse(输入、输出和))
{
WriteLine(“无法从您的输入中解析十进制数。”);
返回;
}

如果(sum数据类型未写入控制台。只能从控制台输入中检索字符串。什么类型的字符串
“2”
-十进制、整数、字节、字符串?您所能做的就是尝试从输入字符串中解析某些类型:

Int32.TryParse("2", out value)
对于您的情况:

Console.WriteLine("Put in the price of the product");
string input = Console.ReadLine();
decimal sum;
if (!Decimal.TryParse(input, out sum))
{
    Console.WriteLine("Decimal number cannot be parsed from your input.");
    return;
}

if (sum <= 100)
    Console.WriteLine("Your final price is {0:0:00}", sum * 0.90M);
Console.WriteLine(“输入产品价格”);
字符串输入=Console.ReadLine();
十进制和;
if(!Decimal.TryParse(输入、输出和))
{
WriteLine(“无法从您的输入中解析十进制数。”);
返回;
}
如果(sum尝试此操作(注意while/break配对):

while(true)
{
字符串输入=Console.ReadLine();
十进制和;
if(十进制色数(输入、输出和)=true)
{
如果(sum尝试此操作(注意while/break配对):

while(true)
{
字符串输入=Console.ReadLine();
十进制和;
if(十进制色数(输入、输出和)=true)
{

如果(sum我相信,如果您使用的转换函数无法将传递的字符串转换为请求的类型,它将引发异常。通常,应避免异常以控制程序流,并为真正意外的情况保留异常。相反,您应该使用不引发异常的方法,而是使用retURN值表示成功或失败。在ind中使用此值,您可以尝试:

    try
    {
        Console.WriteLine("Put in the price of the product");
        decimal sum;
        // Repeat forever (we'll break the loop once the user enters acceptable data)
        while (true)
        {
            string input = Console.ReadLine();
            // Try to parse the input, if it succeeds, TryParse returns true, and we'll exit the loop to process the data.
            // Otherwise we'll loop to fetch another line of input and try again
            if (decimal.TryParse(input, out sum)) break;
        }

        if (sum <= 100)
        {
            decimal totalprice = sum * .90m;
            Console.WriteLine("Your final price is {0:0:00}", totalprice);
        }
    }
    catch
    {

    }
试试看
{
Console.WriteLine(“输入产品价格”);
十进制和;
//永远重复(一旦用户输入可接受的数据,我们将打破循环)
while(true)
{
字符串输入=Console.ReadLine();
//尝试解析输入,如果成功,TryParse将返回true,我们将退出循环以处理数据。
//否则,我们将循环获取另一行输入并重试
if(十进制三分位(输入、输出和))中断;
}

如果(sum我相信,如果您使用的转换函数无法将传递的字符串转换为请求的类型,它将引发异常。通常,应避免异常以控制程序流,并为真正意外的情况保留异常。相反,您应该使用不引发异常的方法,而是使用retURN值表示成功或失败。在ind中使用此值,您可以尝试:

    try
    {
        Console.WriteLine("Put in the price of the product");
        decimal sum;
        // Repeat forever (we'll break the loop once the user enters acceptable data)
        while (true)
        {
            string input = Console.ReadLine();
            // Try to parse the input, if it succeeds, TryParse returns true, and we'll exit the loop to process the data.
            // Otherwise we'll loop to fetch another line of input and try again
            if (decimal.TryParse(input, out sum)) break;
        }

        if (sum <= 100)
        {
            decimal totalprice = sum * .90m;
            Console.WriteLine("Your final price is {0:0:00}", totalprice);
        }
    }
    catch
    {

    }
试试看
{
Console.WriteLine(“输入产品价格”);
十进制和;
//永远重复(一旦用户输入可接受的数据,我们将打破循环)
while(true)
{
字符串输入=Console.ReadLine();
//尝试解析输入,如果成功,TryParse将返回true,我们将退出循环以处理数据。
//否则,我们将循环获取另一行输入并重试
if(十进制三分位(输入、输出和))中断;
}

如果(sum)他是正确的,但为了给你一个更一般的答案…C#有“return”语句,你可以用它来退出一个函数(可能还返回一个值),在你做decimal sum=Convert.ToDecimal(input)的部分;您可以在try/catch块中仅围绕该行,并简单地说在catch块中返回。这将是更简洁的代码,因为它减少了try/catch的范围,这通常更容易理解。您只能从控制台检索字符串。之后,您可以尝试将该字符串解析为另一种类型的实例。Exa示例:
if(int.TryParse(s,out i)){/*对int做一些特殊的事情,比如跳过其他条件*/}
示例代码在这里:他是正确的,但要给您一个更一般的答案……C#有“return”语句,您可以使用这些语句退出函数(并可能返回一个值)在执行十进制求和=Convert.ToDecimal(输入)的部分中;您可以在try/catch块中仅围绕该行,并简单地说在catch块中返回。这将是更简洁的代码,因为它减少了try/catch的范围,这通常更容易理解。您只能从控制台检索字符串。之后,您可以尝试将该字符串解析为另一种类型的实例。Exa示例:
if(int.TryParse(s,out i)){/*为int做一些特殊的事情,比如跳过其他条件*/}
示例代码在这里:你可以省略*==true*部分,它会起作用的。我知道它也会起作用。但我喜欢明确的东西。特里帕斯返回布尔。true是布尔。对吗?但感谢分享你的意见。它们真的非常有用。@SteveWellens这似乎与我相反。如果while是真的,我不会吗Don’我不会永远继续下去?或者这(是真的)像是规则的一个例外吗?它确实起作用了。光标只是向下移动了一行:@Sarah似乎是这样