- //基本計算機
- //System.Convert.ToInt32轉整數
- int num1, num2;
- System.Console.Write("請輸入第一個數:");
- num1 = System.Convert.ToInt32(System.Console.ReadLine());
- System.Console.Write("請輸入第二個數:");
- //若為浮點數會出錯
- num2 = System.Convert.ToInt32(System.Console.ReadLine());
- System.Console.WriteLine(num1+" + "+num2+" = "+(num1+num2));
複製代碼- //基本計算機
- //System.Convert.ToInt32轉整數
- double num1, num2;
- System.Console.Write("請輸入第一個數:");
- num1 = System.Convert.ToDouble(System.Console.ReadLine());
- System.Console.Write("請輸入第二個數:");
- num2 = System.Convert.ToDouble(System.Console.ReadLine());
- System.Console.WriteLine(num1 + " + " + num2 + " = " + (num1 + num2));
- System.Console.WriteLine(num1 + " - " + num2 + " = " + Math.Round(num1 - num2,2));
- System.Console.WriteLine(num1 + " * " + num2 + " = " + (num1 * num2));
- System.Console.WriteLine(num1 + " / " + num2 + " = " + Math.Round(num1 / num2,2));
- System.Console.WriteLine(num1 + " % " + num2 + " = " + Math.Round(num1 % num2),2);
複製代碼 |