如果每次“购买”都在新行中,我的代码会添加总和,但我只希望它们能够进行 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/