Board logo

標題: [3-2]基本計算機 [打印本頁]

作者: 李泳霖    時間: 2023-11-13 12:06     標題: [3-2]基本計算機

  1. //基本計算機
  2. //System.Convert.ToInt32轉整數

  3. int num1, num2;
  4. System.Console.Write("請輸入第一個數:");
  5. num1 = System.Convert.ToInt32(System.Console.ReadLine());
  6. System.Console.Write("請輸入第二個數:");
  7. //若為浮點數會出錯
  8. num2 = System.Convert.ToInt32(System.Console.ReadLine());
  9. System.Console.WriteLine(num1+" + "+num2+" = "+(num1+num2));
複製代碼
  1. //基本計算機
  2. //System.Convert.ToInt32轉整數

  3. double num1, num2;
  4. System.Console.Write("請輸入第一個數:");
  5. num1 = System.Convert.ToDouble(System.Console.ReadLine());
  6. System.Console.Write("請輸入第二個數:");
  7. num2 = System.Convert.ToDouble(System.Console.ReadLine());
  8. System.Console.WriteLine(num1 + " + " + num2 + " = " + (num1 + num2));
  9. System.Console.WriteLine(num1 + " - " + num2 + " = " + Math.Round(num1 - num2,2));
  10. System.Console.WriteLine(num1 + " * " + num2 + " = " + (num1 * num2));
  11. System.Console.WriteLine(num1 + " / " + num2 + " = " + Math.Round(num1 / num2,2));
  12. System.Console.WriteLine(num1 + " % " + num2 + " = " + Math.Round(num1 % num2),2);
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2