数据类型转换

王朝学院·作者佚名  2016-05-16
窄屏简体版  字體: 小  |  中  |  大  |  超大  

数据类型转换好多事情都不如意,感觉好累.有时候想想,那么累是为了什么,不过学习还是得继续,希望自己可以坚持走下去,等明年毕业的同时又能把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)使字符串可以换行.

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航