首页 > 解决方案 > 如何在不使用回车键的情况下按回车键进入菜单

问题描述

所以,我正在学习 C#,为了练习,我一直在尝试制作一个数学求解器,所以我这样做的方式是 1- 我提出数学问题,2- 我收到用户输入的解决方案,3-我使用 if 语句将用户的答案与我的答案进行比较,现在,我正在尝试添加一个控制台菜单来添加不同的除法(乘法/除法/sub./add。),我已经成功添加了菜单,但是我不是能够继续输入数字,我得到的错误是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

标签: c#

解决方案


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

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


推荐阅读