運用了if判斷語句和else if 語句
為了在控制檯迴圈執行,在程式碼的外圍加了for的無限迴圈語句。
for的無限迴圈語句:
for( ; ; )
{
迴圈體;
}
通過將for語句中的初始表示式,迴圈條件,迴圈後執行的表示式置空,實現無限迴圈。
using System;
namespace calculator
{
class Program
{
static void Main(string[] args)
{
for ( ; ; )
{
Console.WriteLine("輸入第一個數");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("計算符號:(+ - * / %)");
String b = Console.ReadLine();
Console.WriteLine("輸入第二個數");
int c = int.Parse(Console.ReadLine());
int d;
if (b == "+")
{
d = a + c;
Console.WriteLine("輸出結果為" + d);
}
else if (b == "-")
{
d = a - c;
Console.WriteLine("輸出結果為" + d);
}
else if (b == "*")
{
d = a * c;
Console.WriteLine("輸出結果為" + d);
}
else if (b == "/")
{
d = a / c;
Console.WriteLine("輸出結果為" + d);
}
else if (b == "%")
{
d = a % c;
Console.WriteLine("輸出結果為" + d);
}
}
}
}
}
執行結果:
輸入第一個數
5
計算符號:(+ - * / %)
*
輸入第二個數
5
輸出結果為25
輸入第一個數
4
計算符號:(+ - * / %)
+
輸入第二個數
6
輸出結果為10
輸入第一個數