Use for-loop command to output the following result:
The user can have 3 times to enter the radius of a circle and the program will calculation the area of the circles.
After 3 times, the program will show "end of game".
這題目該使用有三次機會輸入,需要使用一個迴圈而已。
建議寫法如下:
以上的程式寫法使用了2個變量(variable),i用來計算迴圈的次數,r用來貯存使用者輸入的圓形半徑(radius)的值。
說明:(部份行)
|
行 |
說明 |
|
6 |
迴圈的寫法。由於祇可以輸入3次,因此中間是i<=3。i起始值是1,迴圈一直運作直至大於3。 |
|
8 |
重溫輸入的指令(scanf)而已。scanf("%d",&r); 的作用是讓使用者輸入數值,格式是整數(integer),因此前面用"%d",然後貯存於r的變數(variable)內。留意,r前必需加上&。 |
|
10 |
由於迴圈內有多句指令,因此要使用{ },把指令包圍起來。 |
|
11 |
迴圈完結後,即已執行3次,便輸出這語句。 |
沒有留言:
張貼留言