简体   繁体   English

如何按回车键进入菜单而不使用回车键进行下一步操作

[英]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?所以,我正在学习 C#,为了练习,我一直在尝试制作一个数学求解器,所以我的方法是 1- 我提出数学问题,2- 我收到用户输入的解决方案,3-我使用 if 语句将用户的答案与我的答案进行比较,现在,我正在尝试添加控制台菜单以添加不同的部门(乘法/除法/子。/添加。),我已成功添加了菜单,但我没有能够继续输入数字,我得到的错误是http://prntscr.com/ohru2i ,我该如何解决?

I have tried putting Console.clear(), I have also tried to use break;, but none of them worked我试过把 Console.clear(),我也试过使用 break;,但没有一个工作

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我得到的错误信息是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.将数字转换为字符串时会出现错误,因为 console.Read() 使用标准输入中的第一个字符,但在用户按 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.解决方案是使用 Console.ReadLine() 并通过索引字符串来查看第一个字符,或者用字符串常量替换选择字符常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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