跳出一个 while 循环并进入另一个 c#
Break out of a while loop and into another c#
我的 while 循环有点卡住了。代码的两个部分都可以独立运行,但是,我似乎无法使两个 while 循环都运行。非常感谢任何帮助:
using System;
namespace week5task3 {
class Program {
public static void Main(string[] args)
{
double PriceEvery1 = 5;
double PriceEvery2 = 4;
double PriceEvery3 = 2.5;
double Quantity = 10;
int UserCounter = 0;
Console.WriteLine("\n\nWidget Price Chart\n\n");
Console.WriteLine("Quantity of Widgets\t\tPrice");
while (UserCounter <= 100) {
double Price1 = PriceEvery1 * Quantity;
double Price2 = PriceEvery2 * Quantity;
double Price3 = PriceEvery3 * Quantity;
if (Quantity <= 50) {
Console.WriteLine("\t{0}\t\t\t{1:C}", Quantity, Price1);
}
if (Quantity >= 51 && Quantity <= 80) {
Console.WriteLine("\t{0}\t\t\t{1:C}", Quantity, Price2);
}
if (Quantity >= 81 && Quantity <= 100) {
Console.WriteLine("\t{0}\t\t\t{1:C}", Quantity, Price3);
}
Quantity += 10;
}
while (UserCounter >= 0) {
try {
Console.WriteLine("Enter the amount of widgets you would like to purchase or press q to quit");
string temp = Console.ReadLine();
if (temp == "q") break;
if (temp == "Q") break;
int.TryParse(temp, out UserCounter);
double UserPrice;
if (UserCounter <= 50) {
UserPrice = UserCounter * 5;
Console.WriteLine("The price is {0:C}", UserPrice);
}
if (UserCounter >= 51 && UserCounter <= 80) {
UserPrice = UserCounter * 4;
Console.WriteLine("The price is {0:C}", UserPrice);
}
if (UserCounter > 80) {
UserPrice = UserCounter * 2.5;
Console.WriteLine("The price is {0:C}", UserPrice);
}
} catch (Exception) {
Console.WriteLine("You have entered an incorrect value. Please enter a number or press q to quit");
}
}
}
}
}
使用 while (Quantity <= 100)
而不是 UserCounter <= 100
。这将在打印价格 10 次迭代后退出循环。
我的 while 循环有点卡住了。代码的两个部分都可以独立运行,但是,我似乎无法使两个 while 循环都运行。非常感谢任何帮助:
using System;
namespace week5task3 {
class Program {
public static void Main(string[] args)
{
double PriceEvery1 = 5;
double PriceEvery2 = 4;
double PriceEvery3 = 2.5;
double Quantity = 10;
int UserCounter = 0;
Console.WriteLine("\n\nWidget Price Chart\n\n");
Console.WriteLine("Quantity of Widgets\t\tPrice");
while (UserCounter <= 100) {
double Price1 = PriceEvery1 * Quantity;
double Price2 = PriceEvery2 * Quantity;
double Price3 = PriceEvery3 * Quantity;
if (Quantity <= 50) {
Console.WriteLine("\t{0}\t\t\t{1:C}", Quantity, Price1);
}
if (Quantity >= 51 && Quantity <= 80) {
Console.WriteLine("\t{0}\t\t\t{1:C}", Quantity, Price2);
}
if (Quantity >= 81 && Quantity <= 100) {
Console.WriteLine("\t{0}\t\t\t{1:C}", Quantity, Price3);
}
Quantity += 10;
}
while (UserCounter >= 0) {
try {
Console.WriteLine("Enter the amount of widgets you would like to purchase or press q to quit");
string temp = Console.ReadLine();
if (temp == "q") break;
if (temp == "Q") break;
int.TryParse(temp, out UserCounter);
double UserPrice;
if (UserCounter <= 50) {
UserPrice = UserCounter * 5;
Console.WriteLine("The price is {0:C}", UserPrice);
}
if (UserCounter >= 51 && UserCounter <= 80) {
UserPrice = UserCounter * 4;
Console.WriteLine("The price is {0:C}", UserPrice);
}
if (UserCounter > 80) {
UserPrice = UserCounter * 2.5;
Console.WriteLine("The price is {0:C}", UserPrice);
}
} catch (Exception) {
Console.WriteLine("You have entered an incorrect value. Please enter a number or press q to quit");
}
}
}
}
}
使用 while (Quantity <= 100)
而不是 UserCounter <= 100
。这将在打印价格 10 次迭代后退出循环。