C#程序验证

时间:2015-10-08 21:24:19

标签: c# validation

我需要一些帮助,确保当用户输入任何内容或不输入数字时,它不会崩溃。我把它弄下来,如果他们没有,它会告诉他们输入一个数字,但我不知道如何让它回到原来的问题并给他们一个正确输入的机会。谢谢你的帮助。

        Console.WriteLine("How much is rent: ");
        string  strRent = Console.ReadLine();
        double dblRent = 0.0;
        if (double.TryParse(strRent, out dblRent))
        {
            Console.WriteLine("How much is the car payment: ");
            string strCarPayment = Console.ReadLine();
            double dblCarPayment = Convert.ToDouble(strCarPayment);
        }
        else
        {
            Console.WriteLine("Enter a number");
        }

        Console.WriteLine("How much is student loan payment: ");
        string strStudentLoan = Console.ReadLine();

        Console.WriteLine("How much is phone bill: ");
        string strPhoneBill = Console.ReadLine();

        Console.WriteLine("How much is electric bill: ");
        string strElectricBill = Console.ReadLine();

        Console.WriteLine("Fraction deposited: ");
        string strFractionDeposited = Console.ReadLine();

        Console.WriteLine("Amount leftover: ");
        string strAmountLeft = Console.ReadLine();



        double dblStudentLoan = Convert.ToDouble(strStudentLoan);
        double dblPhoneBill = Convert.ToDouble(strPhoneBill);
        double dblElectricBill = Convert.ToDouble(strElectricBill);
        double dblFractionDeposited = Convert.ToDouble(strFractionDeposited);
        double dblAmountLeft = Convert.ToDouble(strAmountLeft);

        double dblBillSum = dblRent + dblCarPayment + dblStudentLoan + dblPhoneBill + dblElectricBill;
        double afterBills = dblAmountLeft / (1 - dblFractionDeposited);
        double totalPaycheck = afterBills + dblBillSum;
        Console.WriteLine("Total Paycheck: " + totalPaycheck.ToString("C"));

        Console.WriteLine("Enter wage $/hr: ");
        string strWage = Console.ReadLine();

        Console.WriteLine("Enter hours worked overtime: ");
        string strHoursOT = Console.ReadLine();

        Console.WriteLine("Enter overtime multiplier: ");
        string strOTWage = Console.ReadLine();

        double dblWage = Convert.ToDouble(strWage);
        double dblHoursOT = Convert.ToDouble(strHoursOT);
        double dblOTWage = Convert.ToDouble(strOTWage);

        double OTPay = (dblWage * dblOTWage) * dblHoursOT;
        Console.WriteLine("Overtime Pay: " + OTPay.ToString("C"));
        Console.ReadLine();

    }
}

}

3 个答案:

答案 0 :(得分:2)

你需要一个循环,就像这样

        Console.WriteLine("How much is rent: ");
        string strRent = Console.ReadLine();
        double dblRent = 0.0;
        while (!double.TryParse(strRent, out dblRent))
        {
            Console.WriteLine("Enter a number");
            strRent = Console.ReadLine();
        }
        Console.WriteLine("How much is the car payment: ");
        string strCarPayment = Console.ReadLine();
        double dblCarPayment = Convert.ToDouble(strCarPayment);

答案 1 :(得分:0)

试试这个



        double dblRent = 0.0;
        Boolean valid = false;
        while (!valid)
        {
            if (double.TryParse(strRent, out dblRent))
            {
                Console.WriteLine("How much is the car payment: ");
                string strCarPayment = Console.ReadLine();
                double dblCarPayment = Convert.ToDouble(strCarPayment);
                valid = true;
            }
            else
            {
                Console.WriteLine("Enter a number");
            }
        }​




答案 2 :(得分:0)

由于您正在阅读许多双打,因此您可以创建一个封装读取和处理数字的方法

private static double? ReadLineDouble()
{
    while(true) {
        string s = Console.ReadLine();
        if (String.IsNullOrWhitespace(s)) {
            return null; // The user wants to abort
        }
        double d;
        if (Double.TryParse(s, out d)) {
            return d;
        }
        Console.WriteLine("Please enter a valid number");
    }
}

如果用户只是点击Enter,他可能想要中止。在这种情况下,该方法返回null。这是可能的,因为返回类型是Nullable<double>。它的简写符号是double?

现在你可以阅读这样的数字:

double? rent = ReadLineDouble();
if (double == null) return; // Abort the program.
// Otherwise continue.