簡體   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