如果每个“购买”都在新行中,我的代码会加上总和,但我只希望它们能够输入5条。将此代码复制并粘贴到Visual Studio中作为控制台应用程序,然后对其进行调试或运行。我是C#的新手,并且想知道如何只允许5个数字。我正在尝试不要偏离下面的代码。话虽这么说,也许有一种更高级的方法可以做到这一点,但我不希望这样做。
namespace SumFiveDoubles
{
class TotalPurchase
{
static void Main()
{
double purchase;
double total = 0;
string inputString;
const double QUIT = 0;
Console.Write("Enter a purchase amount >> ");
inputString = Console.ReadLine();
purchase = Convert.ToDouble(inputString);
while (purchase != QUIT)
{
total += purchase;
Console.Write("Enter another purchase amount or " + QUIT + " to calculate >> "); //I only want this to appear 4 more times\\
inputString = Console.ReadLine();
purchase = Convert.ToDouble(inputString);
}
Console.WriteLine("Your total is {0}", total.ToString("C"));
}
}
}
最佳答案
int count = 0;
while (purchase != QUIT && ++count < 5)
关于c# - 如何限制C#中循环的次数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22019267/