任何人都可以帮我解决这个简单的For Loop吗?

时间:2016-11-06 15:39:07

标签: c# for-loop

return Json(cart.ApplyFormattingTo("TotalPrice"));

2 个答案:

答案 0 :(得分:0)

你需要一个循环start< end,每次满足条件时,计数器都会增加increment

这将是你的循环结构:

for (int i = start; i < end; i += increment) 
{
    // Add to total and display current value
}

答案 1 :(得分:0)

我稍微修改了你的代码

/* Write a program that asks the user
 * to enter the starting point and end
 * point of the counting range and the 
 * increment value and displays the total 
 * of the numbers within that range
 */

int start;
int end;
int increment;
int sum = 0;
int count= 0;

Console.WriteLine(" Enter the start number ");
start = Int32.Parse(Console.ReadLine());

Console.WriteLine(" Enter the end number ");
end = Int32.Parse(Console.ReadLine());

Console.WriteLine(" Enter the increment number ");
increment = Int32.Parse(Console.ReadLine());

for ( count = start; //init value for count
      count <= end ; //check every loop. if count still satify condition, then do thing inside tho loop
      count += increment //change count every a loop done
    )
{
    sum += count; 
    Console.WriteLine(" Number is: " + count);

}

Console.WriteLine(" Sum is: " + sum);
Console.ReadKey();