好多事情都不如意,感覺好累.有時候想想,那么累是為了什么,不過學習還是得繼續,希望自己可以堅持走下去,等明年畢業的同時又能把C#自學好.
int/double/string 變量 = Console.ReadLine();
int/double/string 變量1 = Convert.ToInt32(變量);
上面兩程序等價于下面的程序
int/double/string 變量1=Convert.TOInt32(Consol.ReadLine());------輸入變量后將變量轉化成相應的類型并把變量的值賦給變量1.
該語句表示將輸入的待轉換的字符轉換成int/double/string類型后賦值給變量.
Convert.ToInt32();轉換為32位有符號的int類型.
try
{
有可能出現錯誤轉換
}
catch
{
如果try中出現異常,就轉入catch中
}
C#中異常捕獲方法.

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 求成績總和和平均分{ class PRogram { static void Main(string[] args) { //Console.WriteLine("請輸入你的語文成績!"); //int chinese = Convert.ToInt32(Console.ReadLine()); //Console.WriteLine("請輸入你的數學成績!"); //int math = Convert.ToInt32(Console.ReadLine()); //Console.WriteLine("請輸入你的英語成績!"); //int english = Convert.ToInt32(Console.ReadLine()); //double avg = (chinese + math + english) / 3; //Console.WriteLine("你的成績總分是:{0} 平均分是:{1}",chinese+math+english,avg); try { Console.WriteLine("請輸入你的名字!"); string name = Console.ReadLine(); Console.WriteLine("請輸入你的語文成績!"); double chinese = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("請輸入你的數學成績!"); double math = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("請輸入你的英語成績!"); double english = Convert.ToInt32(Console.ReadLine()); //double avg = (chinese + math + english) / 3; //Console.WriteLine("{0}你的總分數是:{1}分 平均分是:{2}分",name,chinese + math + english, avg); Console.WriteLine("{0}你的總分數是:{1}分 平均分是:{2}分", name, chinese + math + english, (chinese + math + english) / 3); } catch { Console.WriteLine("你剛輸入的數據有問題,請重新運行!"); } Console.ReadKey(); } }}View Code 
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 天數轉周數{ class Program { static void Main(string[] args) { //作業1 //Console.WriteLine("請輸入一個你要計算的天數!"); //int days = Convert.ToInt32(Console.ReadLine()); //int week = days / 7; //int day = days % 7; //Console.WriteLine("你輸入的{0}天總共是:{1}周零{2}天.",days,week,day); //Console.ReadKey(); //作業2 // try // { // Console.WriteLine("請輸入你要計算的天數!"); // int days = Convert.ToInt32(Console.ReadLine()); // int month = days / 30; // int week = (days - month * 30) / 7; // int day = days - month * 30 - week * 7;//除去月份周數還剩下的天數. // Console.WriteLine("{0}天有{1}個月{2}周零{3}天.", days, month, week, day); //} //catch // { // Console.WriteLine("你輸入的數據有問題,請重新運行程序."); // } // Console.ReadKey(); Console.WriteLine("輸入一個要計算的秒數!"); int seconds = Convert.ToInt32(Console.ReadLine()); // int seconds = 107653; int days = seconds / (3600 * 24); int remainSecond = seconds % (3600 * 24);//除去上面的天數還剩下的秒數. int hour = remainSecond / 3600;//看看剩下的秒數有多少個3600秒 remainSecond = remainSecond % 3600;//除去上面的小時還剩下的分鐘 int min = remainSecond / 60;//剩下的秒數有多少分鐘 int second = remainSecond % 60; Console.WriteLine("{0}秒中一共包含{1}天{2}小時{3}分鐘{4}秒.",seconds,days,hour,min,second); Console.ReadKey(); } }}View Code 轉義字符:/n 換行 /t制表符
/b退格 // 輸出"/"
@在字符串前面加一個@符號,有兩種含義:(1)字符串中如果有/,則不理解為轉義符.(2)使字符串可以換行.
新聞熱點
疑難解答