C#允许用户再次输入,直到正确 [英] c# allow user to input again until correct

查看:39
本文介绍了C#允许用户再次输入,直到正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#制作ATM.其功能之一是让用户在他们的帐户之间转移资金.我如何做到这一点,以便如果用户输入了无效的转账金额(例如负数),系统将提示用户再次输入金额,直到有效为止?我尝试使用while循环,但一旦输入负值行请输入有效金额以进行转帐",就一直不停地重复.

I am making an ATM in c#. One of its functions is letting a user transfer money between their accounts. How can I make it so that if the user enters an invalid amount to transfer (such as a negative amount), the user will be prompted to enter an amount again until it is valid? I tried using a while loop but as soon as I entered a negative value line "Please enter a valid amount to transfer" just kept repeating nonstop.

Console.WriteLine("How much would you like to transfer?");
                double transferamt = double.Parse(Console.ReadLine());
                if (transferamt < 0)
                {
                    Console.WriteLine("Please enter a valid amount to transfer");

                }              

推荐答案

使用 double.TryParse .这样可以确保在用户输入无效格式时不会抛出异常.根据解析的成功将其循环包装.

Use double.TryParse. This ensure no exeption is thrown if the user enters an invalid format. Wrap this in a loop based on the success of the parse.

bool valid = false;
double amount;

while (!valid) 
{
    Console.WriteLine("How much would you like to transfer?");

    valid = double.TryParse(Console.ReadLine(), out amount);
} 

您将需要为负值添加其他验证:

You will need to add additional validation for negative values:

bool valid = false;
double amount;

while (!valid) 
{
    Console.WriteLine("How much would you like to transfer?");

    valid = double.TryParse(Console.ReadLine(), out amount)
        && amount > 0;
} 

C#仅处理确定输出所需的表达式部分.因此,在上面的示例中,如果 double.TryParse(...)返回false,则 amount>0 不会被评估,因为 false&&任何==假.

C# processes only the parts of an expression that are required to determine the output. So in the example above, if double.TryParse(...) returns false, amount > 0 will not be evaluated because false && anything == false.

double.Parse 将引发异常.如果您的.NET版本中没有 double.TryParse ,您可以这样编写自己的文件:

double.Parse will throw an exception if the value is not a valid double. If double.TryParse is not available in your version of .NET you can write your own like so:

public bool TryParse(string value, out double output)
{
    output = 0;

    try
    {
        double = double.Parse(value);
    }
    catch (Exception ex)
    {
        return false;
    }
}

如果您希望在以下尝试中使用其他消息,则可以将其略微重写为:

If you want a different message for the following attempts, you can rewrite it slightly to:

double amount;

Console.WriteLine("How much would you like to transfer?");
bool valid = double.TryParse(Console.ReadLine(), out amount)
    && amount > 0;

while (!valid) 
{        
    Console.WriteLine("Please enter a valid amount to transfer?");
    valid = double.TryParse(Console.ReadLine(), out amount)
        && amount > 0;
} 

这可以重构为:

void Main()
{
    double amount = GetAmount();
}
double GetAmount()
{
    double amount = 0;
    bool valid = TryGetAmount("How much would you like to transfer?", out amount);

    while (!valid) 
    {        
        valid = TryGetAmount("Please enter a valid amount to transfer?", out amount);
    }

    return amount;
}
bool TryGetAmount(string message, out double amount)
{
    Console.WriteLine(message);
    return double.TryParse(Console.ReadLine(), out amount)
        && amount > 0;
}

这篇关于C#允许用户再次输入,直到正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆