# 利用迴圈語法 for, while 及 do while 輸出等差數列及其和
> [name=Junter.wjs] STUST MIS 丙 4A990025
![](https://i.imgur.com/adc9y5G.png)
---
### (s字串) 寫在外層 Form
```csharp=
public partial class Form1 : Form
{
string s;
```
### for 迴圈
```csharp=
private void button1_Click(object sender, EventArgs e)
{
int start, max, step, i, sum = 0;
start = Convert.ToInt32(textBox1.Text);
max = Convert.ToInt32(textBox2.Text);
step = Convert.ToInt32(textBox3.Text);
label8.Text = "";
s = "數列為:";
if (step < 0)
{
if (start < max)
{
MessageBox.Show("由小到大,等差須為正值", "錯誤!", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
else
{
for (i = start; i >= max; i += step)
{
sum -= i;
s += i + " ";
}
s += "\n" + "總和為:" + Math.Abs(sum);
label8.Text = s;
}
}
else
{
if (start > max)
{
MessageBox.Show("由大到小,等差須為負值", "錯誤!", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
else
{
for (i = start; i <= max; i += step)
{
sum += i;
s += i + " ";
}
s += "\n" + "總和為:" + Math.Abs(sum);
label8.Text = s;
}
}
}
```
### do...while 迴圈
```csharp=
private void button2_Click(object sender, EventArgs e)
{
int start, max, step, sum = 0;
start = Convert.ToInt32(textBox1.Text);
max = Convert.ToInt32(textBox2.Text);
step = Convert.ToInt32(textBox3.Text);
label8.Text = "";
s = "數列為:";
if (step < 0)
{
if (start < max)
{
MessageBox.Show("由小到大,等差須為正值", "錯誤!", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
else
{
do
{
sum += start;
s += start + " ";
start += step;
} while (start >= max);
s += "\n" + "總和為:" + Math.Abs(sum);
label8.Text = s;
}
}
else
{
if (start > max)
{
MessageBox.Show("由大到小,等差須為負值", "錯誤!", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
else
{
do
{
sum += start;
s += start + " ";
start += step;
} while (start <= max);
s += "\n" + "總和為:" + Math.Abs(sum);
label8.Text = s;
}
}
}
```
### While 迴圈
```csharp=
private void button3_Click(object sender, EventArgs e)
{
int start, max, step, sum = 0;
start = Convert.ToInt32(textBox1.Text);
max = Convert.ToInt32(textBox2.Text);
step = Convert.ToInt32(textBox3.Text);
label8.Text = "";
s = "數列為:";
if (step < 0)
{
if (start < max)
{
MessageBox.Show("由小到大,等差須為正值", "錯誤!", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
else
{
while (start >= max)
{
sum += start;
s += start + " ";
start += step;
}
s += "\n" + "總和為:" + Math.Abs(sum);
label8.Text = s;
}
}
else
{
if (start > max)
{
MessageBox.Show("由大到小,等差須為負值", "錯誤!", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
else
{
while (start <= max)
{
sum += start;
s += start + " ";
start += step;
}
s += "\n" + "總和為:" + Math.Abs(sum);
label8.Text = s;
}
}
}
```
:::spoiler 【資歷】王俊勝...待更新
個人資料
- 91年次
- 目前大一 就讀資訊管理系
- 常用ID:Junter0323、御痕
經歷:
- [網路遊戲媒體-總編輯](https://acgtalktw.com)
- 台北國際電玩展-記者
- 六都電競賽事-導播
- 2018金象盃全國大數據實務能力競賽-決賽-第一名
- 108學年度教育部全國高級中等學校電機與電子群專題-佳作
- 107年度全國高中職學生電資類群創意競賽-銅牌獎
- 2018愛寶盃創客機器人大賽-佳作
- T貓盃 基礎資安能力實務競賽競賽-特優
- T貓盃 基礎資安能力實務競賽競賽-優勝
- 亞洲機器人大賽-佳作
- 南科嘉南盃AI機器人競賽-佳作
- 愛寶盃創客機器人大賽-佳作
- 來恩盃資訊能力暨創意應用專題競賽-佳作
- 遠大盃全國高中職校超微型創業構想競賽-佳作
未來目標:
- 職業
- 軟硬工程師
- 電競賽事導播
- 課業
- 成就
- FPV 飛手證照
不知道可以幹嘛的證照:
- ICDL 國際資訊安全證照 IT Security
- 乙級 硬體裝修
- 乙級 軟體應用
- 乙級 工業電子
- 丙級 程式設計
- 丙級 硬體裝修
- 丙級 軟體應用
- 丙級 工業電子
$last$ $update: 2021/3/12$
:::
:::warning
程式碼如有錯誤!請自行修正。 :email: 4a990025@stust.edu.tw
:::
###### Tags: `C#` `For` `do..while` `while` `Loop` `迴圈`
Copyright © 2020 Junter.Inc All Rights Reserved