简体   繁体   中英

C# Program Validation

I need some help making sure that when the user enters nothing, or not a number, it doesn't crash. I have it down so that it will tell them to enter a number if they don't, but I don't know how to make it go back to the original question and give them a chance to input correctly. Thanks for the help.

        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();

    }
}

}

You need a loop, something like this

        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);

Try this

  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"); } }​ 

Since you are reading many doubles, you could create a method encapsulating reading and processing the number

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");
    }
}

If the user just hits Enter probably he wants to abort. In this case the method returns null . This is possible, because the return type is a Nullable<double> . The shorthand notation for it is double? .

Now you can read a number like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM