參考答案:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSD01
{
class Program
{
static void Main(string[] args)
{
// TODO
int input1, input2;
string ope = null;
try
{
Console.Write("請輸入第一個數字>");
input1 = int.Parse(Console.ReadLine());
Console.Write("請輸入第二個數字>");
input2 = int.Parse(Console.ReadLine());
do
{
Console.Write("請輸入運算子(+-*/)>");
ope = Console.ReadLine();
if (ope != "+" && ope != "-" && ope != "*" && ope != "/")
Console.WriteLine("運算子必須為+-*/之一");
}while(ope!="+" && ope!="-" && ope!="*" && ope!="/");
if (ope.Equals("+"))
Console.WriteLine("{0}+{1}={2}", input1, input2, input1 + input2);
else if (ope.Equals("-"))
Console.WriteLine("{0}-{1}={2}", input1, input2, input1 - input2);
else if (ope.Equals("*"))
Console.WriteLine("{0}*{1}={2}", input1, input2, input1 * input2);
else if (ope.Equals("/"))
Console.WriteLine("{0}/{1}={2}", input1, input2, input1 / input2);
}
catch (Exception)
{
Console.WriteLine("輸入錯誤,請重新執行");
}
Console.ReadLine();
}
}
}
執行結果:
留言列表