繁体   English   中英

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

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

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

我试过把 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();
                        }
                    }
        }
    }
}

我得到的错误信息是http://prntscr.com/ohru2i

将数字转换为字符串时会出现错误,因为 console.Read() 使用标准输入中的第一个字符,但在用户按 Enter 时留下换行符。 因此,下次从控制台读取一行时,只会得到一个空行,这不是数字转换的有效字符串。

解决方案是使用 Console.ReadLine() 并通过索引字符串来查看第一个字符,或者用字符串常量替换选择字符常量。

暂无
暂无

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

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