【问题标题】:Application Not Working Correctly C# [closed]应用程序无法正常工作 C# [关闭]
【发布时间】:2016-10-14 19:06:12
【问题描述】:

我的代码没有将响应存储为用户输入。每次我到这里时,它都会跳转到 else 语句。然后它把总和设为 50?当我存储输入,例如第一个数字的值 = 2 和第二个数字的值 = 3。 我正在家里上课,我已经工作了几个小时试图弄清楚为什么我的代码不能正常工作。 C#,如下:

                    Console.Write("Would you like to stop adding here (a) or add another number (b)? ");
                    bool stopOrContinueAdding = true;
                    do
                    {
                            string inputMoreNumbers = Console.ReadLine();
                            switch (inputMoreNumbers.ToLower())
                            {
                                case "a":
                                    inputMoreNumbers = "a";
                                    stopOrContinueAdding = false;
                                    break;
                                case "b":
                                    inputMoreNumbers = "b";
                                    stopOrContinueAdding = true;
                                    break;
                                default:
                                    stopOrContinueAdding = false;
                                    break;
                            }
                            if (stopOrContinueAdding == false)
                            {
                                int sum = aNum.Sum();
                                Console.WriteLine("Here is the sum of your numbers: " + sum );
                                System.Threading.Thread.Sleep(4000);
                                Console.WriteLine("Redirecting to main menu...");
                                System.Threading.Thread.Sleep(2000);
                                Console.Clear();
                                Console.Write("Addition(a), Subraction(s), Multiplication(x), or Division(d)? ");
                                isInputValid = false;
                            }
                            else
                            {
                                Console.Clear();
                                Console.Write("Value of next number: ");
                                number++;
                                aNum[number - 1] = Convert.ToInt32(Console.Read());
                                Console.Write("Would you like to stop adding here (a) or add another number (b)? ");
                            }
                        } while (stopOrContinueAdding != true) ;

我不完全确定我哪里出错了。我想了解更多:(

谢谢!

【问题讨论】:

  • 我的代码不工作请更具体。也许访问help center 并阅读有关创建minimal reproducible example 的信息。
  • 当程序询问用户“您要停在这里还是添加另一个号码?”时,我没有让我选择一个选项。它直接进入 else 语句。
  • @Haydan Schweinhund 还有哪个声明?请更具体。

标签: c# loops switch-statement


【解决方案1】:

在您的加法计算器中,更改以下行:

aNum[number - 1] = Convert.ToInt32(Console.Read());

从 Console.Read() 到 Console.ReadLine() 似乎解决了这个问题。 ReadLine() 将阻止执行,直到 Enter 被击中 - Read() 不会......因此,当您输入第二个数字并按 Enter 时,它会执行您的下一个 ReadLine() 语句(您期望“a”或“b " 取决于他们是否要继续)作为一个空白字符串。

【讨论】:

  • 谢谢!现在可以了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多