異常捕獲:

try
{
有可能出現錯誤的代碼寫在這里
}
catch
{
出錯后的處理
}
上面的程序如何運行:
如果try中的代碼沒有出錯,則程序正常運行
try中的代碼,不會執行catch中的代碼;否則
直接跳到catch中執行代碼!



class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("請輸入你的姓名!");
string name = Console.ReadLine();
Console.WriteLine("請輸入你的語文成績:");
int chinese = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入你的數學成績:");
int maths = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入你的英語成績:");
int english = Convert.ToInt32(Console.ReadLine());
int sum = chinese + maths + english;
double average =1.0* (chinese + maths + english) / 3;
Console.WriteLine("{0}!你的總分數為{1},平均分為{2}", name, sum, average);
}
catch
{
Console.WriteLine("你輸入的數據有誤,請重新輸入:");
}
Console.ReadKey();
}
}class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入你要計算的天數:");
int number = Convert.ToInt32(Console.ReadLine());
int week = number / 7;
int day = number % 7;
Console.WriteLine("{0}天是{1}周零{2}天",number,week,day);
}
}class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入你要計算的時間(秒):");
int second = Convert.ToInt32(Console.ReadLine());
int day = second / (24 * 3600);
int hour = second / 3600;
int minute = second / 60;
int second_1 = second % 60;
Console.WriteLine("{0}秒是{1}天{2}小時{3}分{4}秒",second,day,hour,minute,second_1);
}
}運算符
1.自加自減:
++ 自加一 有前加的后加另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網站名稱:C#基礎總結(二)-創新互聯
網站URL:http://www.yijiale78.com/article12/icogc.html
成都網站建設公司_創新互聯,為您提供網站營銷、面包屑導航、微信公眾號、搜索引擎優化、品牌網站建設、手機網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯