VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • C#学习9

14   Convert类型转换

类型如果相兼容的两个变量,可以使用自动类型转换或强制类型转换。但是,如果两个变量的类型不兼容,比如stringintstringdouble,这个时候我们可以使用Convert的转换工厂进行转换。

注意:使用Convert转换也需要注意一个条件:

面上必须要过得去。

复制代码
 1 Console.WriteLine("Hello World!");
 2             string s = "123";
 3             double d = Convert.ToDouble(s);
 4             int v = Convert.ToInt32(s);
 5             Console.WriteLine(d);
 6             Console.WriteLine(v);
 7             Console.WriteLine("请输入你的姓名:");
 8             string name = Console.ReadLine();
 9             Console.WriteLine("请输入你的语文成绩:");
10             string strChinese = Console.ReadLine();
11             Console.WriteLine("请输入您的数学成绩:");
12             string strMath = Console.ReadLine();
13             Console.WriteLine("请输入您的英语成绩:");
14             string strEnglish = Console.ReadLine();
15             //55 77 88
16             //由于字符串相加最终变为相连接,如果要拿字符串类型变量参与计算需要将字符串转换为int/double、
17             int chinese = Convert.ToInt32(strChinese);
18             int math = Convert.ToInt32(strMath);
19             int english = Convert.ToInt32(strEnglish);
20             Console.WriteLine("{0},你的总成绩为{1},平均成绩为{2}", name, chinese + english + math, (chinese + english + math) / 3);
复制代码
复制代码
 1 //47天是几周几天
 2             int n = 47;
 3             int m = n % 7;
 4             int w = n / 7;
 5             Console.WriteLine("47小时表示{0}周{1}天", w, m);
 6             //107653秒是几天几小时几分几秒
 7             int s = 107653;
 8             int q = s / 60 / 60 / 24;
 9             int e = (s - q * 60 * 60 * 24) / 60 / 60;
10             int r = (s - q * 60 * 60 * 24 - e * 60 * 60) / 60;
11             int t = s % 60;
12             Console.WriteLine("表示的是{0}天{1}小时{2}分{3}秒", q, e, r, t);
13             //107653秒是几天几小时几分几秒
14             int s = 107653;
15             int day = 24 * 60 * 60;
16             int days = s / day;
17             int the_next = s % day;
18             int hour = 60 * 60;
19             int hours = the_next / hour;
20             int the_third = the_next % hour;
21             int minute = 60;
22             int minutes = the_third / minute;
23             int seconds = the_third % minute;
24             Console.WriteLine("{0}天{1}小时{2}分{3}秒", days, hours, minutes, seconds);
复制代码

原文链接:https://www.cnblogs.com/interesters-together/p/13648597.html


相关教程