using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _03循环控制 { class Program { static void Main(string[] args) { /* * for(int i = 0; i < 100; i++) * { * * } * * int index= 0; * while(条件) * { * * index++; * } * * do * { * * }while(条件); * * * 转化的方法 * int num = Convert.ToInt32(字符串); * "123a" * * 处理异常 * try * { * 可能出现异常的代码 * } * catch * { * 如果出现了异常,就在异常处停下来,其后的代码不在执行 * 直接跳到catch中执行代码 * } */ // 输入数字,如果不是数字,提示错误,并重新输入 Console.WriteLine("情输入数字"); #region while //string str = Console.ReadLine(); //while (true) //{ // try // { // int num = Convert.ToInt32(str); // Console.WriteLine(num * 2); // break; // } // catch // { // Console.WriteLine("ERROR"); // Console.WriteLine("输入错误,请重新输入"); // str = Console.ReadLine(); // } //} #endregion string str; do { str = Console.ReadLine(); try { int num = Convert.ToInt32(str); Console.WriteLine(num * 2); break; } catch { Console.WriteLine("error"); } } while (true); Console.ReadKey(); } } }