国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

數據類型轉換

2019-11-17 03:19:21
字體:
來源:轉載
供稿:網友

數據類型轉換

好多事情都不如意,感覺好累.有時候想想,那么累是為了什么,不過學習還是得繼續,希望自己可以堅持走下去,等明年畢業的同時又能把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)使字符串可以換行.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 霍邱县| 普兰店市| 区。| 白银市| 乌海市| 衡阳县| 杭锦旗| 龙海市| 凤翔县| 行唐县| 凤台县| 桐乡市| 大姚县| 新津县| 班玛县| 时尚| 乐陵市| 安义县| 阳原县| 陕西省| 兰考县| 天镇县| 凤庆县| 新化县| 陵水| 兰溪市| 翁源县| 镇康县| 西峡县| 丰顺县| 莲花县| 通河县| 卢湾区| 洪泽县| 廊坊市| 射洪县| 仁布县| 新河县| 抚州市| 社旗县| 常熟市|