輸入任意兩個整數,求兩者的商。使用異常處理機制捕獲零除異常和引數格式異常
阿新 • • 發佈:2019-02-06
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("請輸入兩個整數:"); int a, b; double c = 0; try { a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); c = a / b; } catch (FormatException) { Console.WriteLine("輸入格式不正確,請輸入一個整數"); Console.ReadKey(); } catch (DivideByZeroException) { Console.WriteLine("出書不能為0"); Console.ReadKey(); } if (c != 0) { Console.WriteLine(c); Console.ReadKey(); } } } }