這是用C#語言做的一個控制臺的簡易計算器
運用了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
輸入第一個數
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/86211.html
標籤:其他
上一篇:原生JDBC和工具類的基本實作
下一篇:分布式系統中的CAP理論
