简体   繁体   中英

How do I set up exception handling for no input

This is my program for taking in 2 numbers and an operator then doing the math and spitting out the answer(thanks to async for his help previously). So far everything works good except for when I input nothing. I'm trying to get it to say no input, try again, but it just crashes. Does anyone know why? thanks in advance.

using System;
class Calculation
{
    public static void Main(string[] data)
    {

        if (data.Length == 0)
        {
            Console.WriteLine("No input... Try again");

            return;
        }

        double result;

        if (!Double.TryParse(data[0], out result))
        {
            Console.WriteLine("Invalid input: " + data[0]);

            return;
        }

        Console.WriteLine("Starting with number: " + result);

        char op;
        double number;

        string errorMessage;

        do
        {
            if (!TryGetData(out op, out number, out errorMessage))
            {
                Console.WriteLine("Invalid input: " + errorMessage);

                continue;
            }

            switch (op)
            {
                case '+':
                    result += number;
                    break;
                case '-':
                    result -= number;
                    break;
                case '*':
                    result *= number;
                    break;
                case '/':
                    result /= number;
                    break;
                default:
                    Console.WriteLine("Done");
                    continue;
            }

            Console.WriteLine("Result = " + result.ToString());


        } while (Char.ToLower(op) != 'q');
    }

    static bool TryGetData(out char anOperator, out double aNumber, out string message)
    {
        aNumber = 0;
        message = null;

        Console.Write("Enter an operator and a number or 'q' to quit: ");

        var line = Console.ReadLine();

        anOperator = line[0];

        if (anOperator != 'q' && anOperator != 'Q')
        {
            if (line.Length <= 2)
            {
                message = "Enter an operator followed by a space and then a number.";

                return false;
            }

            var isValidNumber = Double.TryParse(line.Substring(2), out aNumber);

            if (!isValidNumber)
            {
                message = "Invalid input: " + line.Substring(2);

                return false;
            }

            if (isValidNumber && (anOperator == '/' && aNumber == 0))
            {
                message = "Cannot divide by 0.";

                return false;
            }

            if (line[1] != ' ')
            {
                message = "Make sure to put a space between operator and number.";

                return false;
            }

        }

        return true;
    }
}

You are telling it to check for the length property on a variable that is null. If you want to remove you can either check if null, and then check for length.

C# is inherently a short-circuiting language, and if the first part of an if statement is false (or true) the other parts wont be evaluated.

if (data == null || data.length == 0) 
{
    Console.WriteLine("No input... Try again");
    return;
}

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