2016-11-06 8 views
1
/* 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(); 
+0

あなたのforループ – yashpandey

+2

間違っているここには質問、ちょうど宿題はありませんし、いくつかのコード。実際に何が期待されているのか、実際の結果、エラーメッセージなどはどういうものなのかを明確にしてください。 –

+0

私はループで数日間のテストを準備しています。これはサンプルの質問です これは私が戻ってくるものです。 開始番号を入力します。1終了番号を入力します。10増分番号を入力します。2番号:0合計:0これはコンソールウィンドウが私に戻ってくるのを促すものですか? –

答えて

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(); 
+0

これは動作しているようです!ありがとうございました ! また、for文について説明できますか? 1.初期化カウント=開始? 2.ガード...カウントが終了未満ですか? 3.処理を進める... count = count + increment –

+0

私は上記のコードを説明しようとしていますが、詳しくはhttps://www.dotnetperls.com/forを参照することをお勧めします。 – zquanghoangz

0

あなたがstart < end whileループをしたい、その条件が満たされるたびに、カウンタがincrementによってインクリメントされます。

これはあなたのループの構造を次のようになります。

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

終了番号は増分番号番号は、入力してください開始番号を入力します:0 合計することである:0 これは、コンソールウィンドウが私に戻って促しているものです? –

+0

ループの前に 'count'を宣言しないでください。ループは 'for(int count = start; count

関連する問題