有人能帮我这个简单的For循环吗?

本文关键字:For 循环 简单 | 更新日期: 2023-09-27 18:16:29

/* 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 ( start = ; end <= start ; count = count + increment  )
{
    Console.WriteLine(" Number is: " + count);
}
Console.WriteLine(" Sum is: " + sum);
Console.ReadKey();

有人能帮我这个简单的For循环吗?

我稍微修改了一下你的代码

/* 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();

start <end,且每次满足该条件时,计数器加increment。>

这将是你的循环结构:

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