【发布时间】: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