1、兩個練習題
1)編程實現46天,是幾周幾天
int days = 46;
int weeks = days / 7;
int day =days % 7;
//Console.WriteLine(weeks);
//Console.WriteLine(day);
Console.WriteLine("{0}些天是{1}個周{2}天",days,weeks,day);
2)實現107653秒是幾天幾時幾秒
int seconds = 107653;
int day = seconds / 86400;
int secs = second % 86400;
int hour = secs / 3600;
secs = sec % 3600;
int mins = secs / 60;
secs = secs % 60;
Console.WriteLine("{0}天{1}小時{2}分鐘",day,hour,mins);
2、類型轉換
Convert類型轉換
eg:
string s = “123”;
double b = Convert.ToDouble(s);
int n = Convert.ToInt32(s);
Console.WriteLine(b);
Console.WriteLine(n);
注意:字符串中又字母時,不會報錯,但運行會語法錯誤
3、問題
提示用戶輸入三門課的成績(語文,數學,英語),然後給用戶顯示總成績和平均分。
Console.writeLine("請輸入你的姓名:");
string name = Console.ReadLine();
Console.WriteLine("請輸入你的語文成績");
string Chinese = Console.ReadLine();
Console.WriteLine("請輸入你的數學成績");
string Math = Console.ReadLine();
Console.WriteLine("請輸入你的英語成績");
string English = Console.ReadLine();
double doubleChinese = Convert.Double(Chinese);
double doubleMath = Convert.Double(Math);
double doubleEnglish = Convert.Double(English);
Console.WriteLine("{0}你的總成績是{1}平均分是{2}",name,doubleChinese+doubleMath+doubleEnglish,
(doubleChinese+doubleMath+doubleEnglish)/3);