我是编程的新手,我只需要多次重复我的代码部分(在我的情况下为100),而不必一次又一次地复制和粘贴它(因为这看起来相当丑陋)。这是有问题的一行:
Console.WriteLine ();
Console.WriteLine ("Q7: x² - 4x + 4 = 0");
int ans8 = Convert.ToInt32 (Console.ReadLine ());
if (ans8 == 2) {
++experiencePoints;
++experiencePoints;
++experiencePoints;
++experiencePoints;
++experiencePoints;
++experiencePoints;
++experiencePoints;
++experiencePoints;
++experiencePoints;
++experiencePoints;
Console.WriteLine ("Correct");
换句话说,我希望程序能够识别'++ experiencePoints'100次而不必像我刚才那样写出来
答案 0 :(得分:1)
您可以使用for
循环:
for(int i = 0; i < 100; ++i)
{
++experiencePoints;
}
或者只是使用:
experiencePoints += 100;
答案 1 :(得分:1)
你想要的是一个for
循环:
for (int i = 0; i < 100; i++)
++experiencePoints;
但是,在您的具体情况下,您只需向experiencePoints
添加100,如下所示:
experiencePoints += 100;
答案 2 :(得分:1)
++experiencePoints
向experiencePoints
添加1,但+=
运算符会将您指定的任何金额添加到变量中。
试试experiencePoints += 100;
,这是experiencePoints = experiencePoints + 100
您不需要一次添加数字1。请注意,使用像其他人一样的for
循环建议,解决了您的问题,但您永远不应该这样做,因为它需要更多的内存和时间。