满足while条件退出循环
Exit loop if while condition is met
我有一个将用户输入存储在数组中的程序。
每次存储用户输入时,int var Counter 都会加 1。
我的问题是,如果我存储了 25 个变量,则循环结束但假设我输入了无效的内容,即使存储了 25 个变量,循环也会继续。我想确保循环在满足 while 条件后结束,即使程序捕获了错误(如果有任何意义)。
不,我想使用数组而不是列表!
谢谢
void AddPassenger()
{
//Counts total passengers on the bus to determin if the buss is empty
Passenger.TotalPassengers = 0;
//Variable for loops
int Counter = 0;
do
{
//Adds 25 passengers with a loop
for (int i = 0; i < Passenger.Age.Length; i++)
{
Console.Clear();
Logo();
Console.WriteLine("\nEnter Age for passenger {0}", Counter + 1);
Counter++;
try
{
Passenger.Age[i] = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Counter--;
Console.ReadKey();
break;
}
//Checks if user enters a valid age
if (Passenger.Age[i] < 0 || Passenger.Age[i] > 130)
{
Console.WriteLine("Try a different age");
}
Console.WriteLine("\nEnter Gender for passenger {0}", Counter);
Console.WriteLine("1) Male");
Console.WriteLine("2) Female");
Console.WriteLine("3) Other");
//Using a temp variable to run switch for selecting gender
int temp = 0;
try
{
temp = Convert.ToInt32(Console.ReadLine());
Console.Clear();
Passenger.TotalPassengers++;
}
catch
{
Console.Clear();
Logo();
Console.WriteLine("\nNot a valid choice");
}
switch (temp)
{
case 1:
Passenger.Gender[i] = "Male";
break;
case 2:
Passenger.Gender[i] = "Female";
break;
case 3:
Passenger.Gender[i] = "Other";
break;
default:
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
Counter--;
break;
}
}
} while (Counter < 25);
这不应该发生
在你的问题范围内,你可以做类似下面的事情(注意这还没有 运行 但很快就引起了评论)
void AddPassenger()
{
//Counts total passengers on the bus to determin if the buss is empty
Passenger.TotalPassengers = 0;
//Variable for loops
int Counter = 0;
do
{
//Adds 25 passengers with a loop
for (int i = 0; i < Passenger.Age.Length; i++)
{
// ensure we're not leaving the scope of the `while`
if(Counter >= 25)
break;
Console.Clear();
Logo();
Console.WriteLine("\nEnter Age for passenger {0}", Counter + 1);
try
{
Passenger.Age[i] = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
break;
}
//Checks if user enters a valid age
if (Passenger.Age[i] < 0 || Passenger.Age[i] > 130)
{
Console.WriteLine("Try a different age");
break; // you needed to break here I assume
}
Console.WriteLine("\nEnter Gender for passenger {0}", Counter);
Console.WriteLine("1) Male");
Console.WriteLine("2) Female");
Console.WriteLine("3) Other");
//Using a temp variable to run switch for selecting gender
int temp = 0;
try
{
temp = Convert.ToInt32(Console.ReadLine());
Console.Clear();
Passenger.TotalPassengers++;
}
catch
{
Console.Clear();
Logo();
Console.WriteLine("\nNot a valid choice");
break; // you needed to break here I assume
}
switch (temp)
{
case 1:
Passenger.Gender[i] = "Male";
break;
case 2:
Passenger.Gender[i] = "Female";
break;
case 3:
Passenger.Gender[i] = "Other";
break;
default:
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
break;
}
Counter++; //move counter to here to only increment at the end if successful
}
} while (Counter < 25);
但是,我会更改 Passenger
的名称或模型本身,因为不清楚为什么 Passenger 有一个年龄或性别数组。当然,每个乘客都应该是具有这些字段的自己的实体吗?
类似于:
public Bus
{
public Passenger[] Passengers { get; set; }
public int TotalPassengers { get; set; }
}
public Passenger
{
public int Age { get; set; }
public string Gender { get; set; }
}
应该重新考虑 while 中的 for
循环。这样的嵌套循环没有意义。
这里有2个循环,一个for循环和一个do-while循环。 for 循环仅在 i
达到数组的限制时停止,而 do-while 循环仅在 Counter
达到 25.
时停止
您提到即使在存储了 25 个变量后循环仍在继续。这是因为每次有无效输入时 for 循环都会从 0 重新开始。将 int i = 0
换成 int i = Counter
,这样,它会从中断处继续。
此外:
您不需要 2 个循环,特别是 for 循环。您可以删除它,然后将所有提及的 i
替换为 Counter
,将 break
替换为 continue
。 (结合@Tubs 的回答)在这种情况下您也不需要 if(Counter >= 25) break;
。这样做有效地合并了 2 个循环,而不是让 2 个循环具有相同的功能。
Console.WriteLine("\nEnter Gender for passenger {0}", Counter);
不会对 Counter
加 1,这将导致 Enter Gender for passenger 0
.
扩展@Tubs 的回答:
此外,Counter 仅在所有 break
之后递增,这意味着它无法访问。您必须转移到每个成功的案例(1、2 和 3)。
结果代码是:
void AddPassenger()
{
//Counts total passengers on the bus to determin if the buss is empty
Passenger.TotalPassengers = 0;
//Variable for loops
int Counter = 0;
//Adds 25 passengers with a loop
do
{
Console.Clear();
Logo();
Console.WriteLine("\nEnter Age for passenger {0}", Counter + 1);
try
{
Passenger.Age[i] = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
continue;
// Continue while loop instead of breaking it
}
//Checks if user enters a valid age
if (Passenger.Age[i] < 0 || Passenger.Age[i] > 130)
{
Console.WriteLine("Try a different age");
continue;
// Continue while loop instead of breaking it
}
Console.WriteLine("\nEnter Gender for passenger {0}", Counter + 1);
Console.WriteLine("1) Male");
Console.WriteLine("2) Female");
Console.WriteLine("3) Other");
//Using a temp variable to run switch for selecting gender
int temp = 0;
try
{
temp = Convert.ToInt32(Console.ReadLine());
Console.Clear();
Passenger.TotalPassengers++;
}
catch
{
Console.Clear();
Logo();
Console.WriteLine("\nNot a valid choice");
continue;
// Continue while loop instead of breaking it
}
switch (temp)
{
case 1:
Passenger.Gender[i] = "Male";
// Increment after success
Counter++;
continue;
// Continue while loop instead of breaking it
case 2:
Passenger.Gender[i] = "Female";
// Increment after success
Counter++;
continue;
// Continue while loop instead of breaking it
case 3:
Passenger.Gender[i] = "Other";
// Increment after success
Counter++;
continue;
// Continue while loop instead of breaking it
default:
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
continue;
// Continue while loop instead of breaking it
}
} while (Counter < 25);
我有一个将用户输入存储在数组中的程序。 每次存储用户输入时,int var Counter 都会加 1。 我的问题是,如果我存储了 25 个变量,则循环结束但假设我输入了无效的内容,即使存储了 25 个变量,循环也会继续。我想确保循环在满足 while 条件后结束,即使程序捕获了错误(如果有任何意义)。
不,我想使用数组而不是列表!
谢谢
void AddPassenger()
{
//Counts total passengers on the bus to determin if the buss is empty
Passenger.TotalPassengers = 0;
//Variable for loops
int Counter = 0;
do
{
//Adds 25 passengers with a loop
for (int i = 0; i < Passenger.Age.Length; i++)
{
Console.Clear();
Logo();
Console.WriteLine("\nEnter Age for passenger {0}", Counter + 1);
Counter++;
try
{
Passenger.Age[i] = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Counter--;
Console.ReadKey();
break;
}
//Checks if user enters a valid age
if (Passenger.Age[i] < 0 || Passenger.Age[i] > 130)
{
Console.WriteLine("Try a different age");
}
Console.WriteLine("\nEnter Gender for passenger {0}", Counter);
Console.WriteLine("1) Male");
Console.WriteLine("2) Female");
Console.WriteLine("3) Other");
//Using a temp variable to run switch for selecting gender
int temp = 0;
try
{
temp = Convert.ToInt32(Console.ReadLine());
Console.Clear();
Passenger.TotalPassengers++;
}
catch
{
Console.Clear();
Logo();
Console.WriteLine("\nNot a valid choice");
}
switch (temp)
{
case 1:
Passenger.Gender[i] = "Male";
break;
case 2:
Passenger.Gender[i] = "Female";
break;
case 3:
Passenger.Gender[i] = "Other";
break;
default:
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
Counter--;
break;
}
}
} while (Counter < 25);
这不应该发生
在你的问题范围内,你可以做类似下面的事情(注意这还没有 运行 但很快就引起了评论)
void AddPassenger()
{
//Counts total passengers on the bus to determin if the buss is empty
Passenger.TotalPassengers = 0;
//Variable for loops
int Counter = 0;
do
{
//Adds 25 passengers with a loop
for (int i = 0; i < Passenger.Age.Length; i++)
{
// ensure we're not leaving the scope of the `while`
if(Counter >= 25)
break;
Console.Clear();
Logo();
Console.WriteLine("\nEnter Age for passenger {0}", Counter + 1);
try
{
Passenger.Age[i] = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
break;
}
//Checks if user enters a valid age
if (Passenger.Age[i] < 0 || Passenger.Age[i] > 130)
{
Console.WriteLine("Try a different age");
break; // you needed to break here I assume
}
Console.WriteLine("\nEnter Gender for passenger {0}", Counter);
Console.WriteLine("1) Male");
Console.WriteLine("2) Female");
Console.WriteLine("3) Other");
//Using a temp variable to run switch for selecting gender
int temp = 0;
try
{
temp = Convert.ToInt32(Console.ReadLine());
Console.Clear();
Passenger.TotalPassengers++;
}
catch
{
Console.Clear();
Logo();
Console.WriteLine("\nNot a valid choice");
break; // you needed to break here I assume
}
switch (temp)
{
case 1:
Passenger.Gender[i] = "Male";
break;
case 2:
Passenger.Gender[i] = "Female";
break;
case 3:
Passenger.Gender[i] = "Other";
break;
default:
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
break;
}
Counter++; //move counter to here to only increment at the end if successful
}
} while (Counter < 25);
但是,我会更改 Passenger
的名称或模型本身,因为不清楚为什么 Passenger 有一个年龄或性别数组。当然,每个乘客都应该是具有这些字段的自己的实体吗?
类似于:
public Bus
{
public Passenger[] Passengers { get; set; }
public int TotalPassengers { get; set; }
}
public Passenger
{
public int Age { get; set; }
public string Gender { get; set; }
}
应该重新考虑 while 中的 for
循环。这样的嵌套循环没有意义。
这里有2个循环,一个for循环和一个do-while循环。 for 循环仅在 i
达到数组的限制时停止,而 do-while 循环仅在 Counter
达到 25.
您提到即使在存储了 25 个变量后循环仍在继续。这是因为每次有无效输入时 for 循环都会从 0 重新开始。将 int i = 0
换成 int i = Counter
,这样,它会从中断处继续。
此外:
您不需要 2 个循环,特别是 for 循环。您可以删除它,然后将所有提及的 i
替换为 Counter
,将 break
替换为 continue
。 (结合@Tubs 的回答)在这种情况下您也不需要 if(Counter >= 25) break;
。这样做有效地合并了 2 个循环,而不是让 2 个循环具有相同的功能。
Console.WriteLine("\nEnter Gender for passenger {0}", Counter);
不会对 Counter
加 1,这将导致 Enter Gender for passenger 0
.
扩展@Tubs 的回答:
此外,Counter 仅在所有 break
之后递增,这意味着它无法访问。您必须转移到每个成功的案例(1、2 和 3)。
结果代码是:
void AddPassenger()
{
//Counts total passengers on the bus to determin if the buss is empty
Passenger.TotalPassengers = 0;
//Variable for loops
int Counter = 0;
//Adds 25 passengers with a loop
do
{
Console.Clear();
Logo();
Console.WriteLine("\nEnter Age for passenger {0}", Counter + 1);
try
{
Passenger.Age[i] = Convert.ToInt32(Console.ReadLine());
}
catch
{
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
continue;
// Continue while loop instead of breaking it
}
//Checks if user enters a valid age
if (Passenger.Age[i] < 0 || Passenger.Age[i] > 130)
{
Console.WriteLine("Try a different age");
continue;
// Continue while loop instead of breaking it
}
Console.WriteLine("\nEnter Gender for passenger {0}", Counter + 1);
Console.WriteLine("1) Male");
Console.WriteLine("2) Female");
Console.WriteLine("3) Other");
//Using a temp variable to run switch for selecting gender
int temp = 0;
try
{
temp = Convert.ToInt32(Console.ReadLine());
Console.Clear();
Passenger.TotalPassengers++;
}
catch
{
Console.Clear();
Logo();
Console.WriteLine("\nNot a valid choice");
continue;
// Continue while loop instead of breaking it
}
switch (temp)
{
case 1:
Passenger.Gender[i] = "Male";
// Increment after success
Counter++;
continue;
// Continue while loop instead of breaking it
case 2:
Passenger.Gender[i] = "Female";
// Increment after success
Counter++;
continue;
// Continue while loop instead of breaking it
case 3:
Passenger.Gender[i] = "Other";
// Increment after success
Counter++;
continue;
// Continue while loop instead of breaking it
default:
Console.WriteLine("Did you enter a number?\n");
Console.WriteLine("Press any key to try again...");
Console.ReadKey();
continue;
// Continue while loop instead of breaking it
}
} while (Counter < 25);