简体   繁体   中英

How to press enter for a menu without the enter being used for the next operation

So, I am learning C#, and to practice, I have been trying to make a math solver, so the way I did it, is 1- I present the math question, 2- I receive user-input of the solution, 3- I compare the user's answer to my answer using an if statement, now, I am trying to add a console menu to add different divisions (multiplication/division/sub./add.), i have successfully added the menu, however I am not able to move onto inputting the numbers, the error I get is http://prntscr.com/ohru2i , how can I fix it?

I have tried putting Console.clear(), I have also tried to use break;, but none of them worked

using Figgle;
using System; 
using System.Threading;

public class MainClass
{
    public static void Main()
    {
        Console.Title = $"The Math Solver | Correct = 0 | Wrong = 0";
        char choice;

        for (; ; )
        {
            do
            {
                Console.WriteLine("Choose Method:");
                Console.WriteLine("  1. Multiplication");
                Console.WriteLine("  2. Division");
                Console.WriteLine("  3. Addition");
                Console.WriteLine("  4. Subtraction");
                Console.WriteLine("  5. Find the Remainder");
                Console.WriteLine("Press Q to Exit ");
                do
                {
                    choice = (char)Console.Read();
                } while (choice == '\n' | choice == '\r');
            } while (choice < '1' | choice > '5' & choice != 'q');

            if (choice == 'q') break;

            Console.WriteLine("\n");
            Console.Clear();
            switch (choice)
            {
                case '1':
                    {
                        Console.WriteLine(
                        FiggleFonts.Standard.Render("Multiplication"));

                        int milliseconds2 = 2000;
                        Thread.Sleep(milliseconds2);

                        int correctAnswers = 0;
                        int WrongAnswers = 0;
                        int Number1;
                        int Number2;
                        int myInt2;
                        while (true)
                        {
                            Console.WriteLine("Write the first number to multiply");
                            Number1 = int.Parse(Console.ReadLine());
                            Console.WriteLine("Write the second number to multiply");
                            Number2 = int.Parse(Console.ReadLine());
                            Console.WriteLine($"Write the answer of {Number1} * {Number2}");
                            myInt2 = int.Parse(Console.ReadLine());

                            if (myInt2 == Number1 * Number2)
                            {
                                Console.WriteLine(
                                FiggleFonts.Standard.Render("Correct!"));
                                correctAnswers++;
                                Console.Title = $"The Math Solver | Correct = {correctAnswers} | Wrong = {WrongAnswers}";
                            }
                            else
                            {
                                Console.WriteLine(
                                FiggleFonts.Standard.Render("Wrong"));
                                WrongAnswers++;
                                Console.Title = $"The Math Solver | Correct = {correctAnswers} | Wrong = {WrongAnswers}";
                            }
                            int milliseconds3 = 2000;
                            Thread.Sleep(milliseconds3);
                            Console.Clear();
                        }
                    }
        }
    }
}

The error message I get is http://prntscr.com/ohru2i

You're getting the error when converting a number to a string because console.Read() consumes the first character from the standard input, but leaves the line break from the user hitting enter. Therefore, the next time you go to read a line from the console, you just get a blank line, which is not a valid string for number conversion.

Solution is to use Console.ReadLine() and either look at the first character by indexing the string, or replace choice character constants with string constants.

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