hihoCoder題二:A + B詳解
阿新 • • 發佈:2018-12-25
using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace ConsoleApp3 { class Program1 { static void Main(string[] args) { List<int> list = new List<int>(); string s; while (true) { if (list.Count==0) { Console.Write("第" + 1 + "組資料為:"); } s = Console.ReadLine();//儲存輸入 //輸入符合要求時 if (Regex.IsMatch(s, "^([1][0][0]|[1-9][0-9]|[1-9]){1}(\\s+)([1][0][0]|[1-9][0-9]|[1-9]){1}$")) { string[] s2 = s.Split(' '); list.Add(Convert.ToInt32(s2[0]) + Convert.ToInt32(s2[1])); Console.Write("第" + (list.Count+1) + "組資料為:"); } else//輸入不符合要求時 { if (String.IsNullOrEmpty(s))//直接回車會輸出之前每組資料答案 { for (int i = 0; i < list.Count; i++) { Console.Write("第" + (i+1) + "組資料之和為:"); Console.WriteLine(list[i]); } list = new List<int>(); } else//輸入錯誤提示 { Console.WriteLine("第" + (list.Count + 1) + "組資料輸入格式有誤"); Console.Write("第" + (list.Count + 1) + "組資料為:"); } } } } } }