jeudi 28 septembre 2017

Getting errors with user console input

I attempted to make a basic menu for the user to select the option they want, but receive an error when the user inputs an answer. Is there any way to make this work/more efficient and still keep it basic?

 Console.WriteLine("Enter '1' to multiply");
        Console.WriteLine("Enter '2' to divide");
        Console.ReadLine();

        int answer;
        answer = Convert.ToInt16(Console.ReadLine());   //get an error in this line

        if (answer == 1)
        {
            int number1;
            int number2;

            Console.WriteLine("You chose to multiply");
            Console.ReadLine();

            Console.Clear();

            Console.WriteLine("Enter a number..");
            number1 = Convert.ToInt16(Console.ReadLine());
            Console.Clear();
            Console.WriteLine("Enter another number..");
            number2 = Convert.ToInt16(Console.ReadLine());
            Console.Clear();

            Console.WriteLine("Your answer is --> " + number1 * number2);
        }

        else if (answer != 1)
        {
            int number3;
            int number4;

            Console.WriteLine("You chose to divide");
            Console.ReadLine();

            Console.Clear();

            Console.WriteLine("Enter a number..");
            number3 = Convert.ToInt16(Console.ReadLine());
            Console.Clear();
            Console.WriteLine("Enter another number..");
            number4 = Convert.ToInt16(Console.ReadLine());
            Console.Clear();

            Console.WriteLine("Your answer is --> " + number3 / number4);
        }

Aucun commentaire:

Enregistrer un commentaire