【发布时间】:2019-10-12 15:53:17
【问题描述】:
我正在尝试打印 if 语句,但前提是值正确且符合要求。但是,if 语句位于 while 循环内,即使它要求用户在 if 语句中的值不正确时重试,它仍然会打印出用户第一次在 if 之外的 WriteLine 中输入的数字声明(它说模拟)。我希望它要求用户再试一次,直到值符合 if 语句的要求标准。
namespace Woodchuck
{
class Program
{
static void Main(string[] args)
{
string restart = "Y";
while (restart == "Y")
{
int totalWoodChucks = 0;
int days = 0;
const int MAXDAYS = 10;
const int MINDAYS = 1;
const int MAXWOOD = 100;
const int MINWOOD = 1;
Write("Enter number of woodchucks for this simulation (1-100): ");
int.TryParse(ReadLine(), out totalWoodChucks);
if (totalWoodChucks > MAXWOOD || totalWoodChucks < MINWOOD)
{
WriteLine("Please try again");
}
Write("Enter number of days for this simulation (1-10): ");
int.TryParse(ReadLine(), out days);
if (days > MAXDAYS || days < MINDAYS)
{
WriteLine("Please try again");
}
WriteLine($"SIMULATION 1: {totalWoodChucks} woodchucks over {days} days");
Write("\nTo run another simulation, enter 'Y':");
restart = ReadLine().ToUpper();
}
//Debug pause
WriteLine("Press any key to wrap it up...");
}
}
}
【问题讨论】:
标签: c# if-statement while-loop