首页 > 学院 > 开发设计 > 正文

数据类型转换

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)使字符串可以换行.


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表