-
C#学习9
14 Convert类型转换
类型如果相兼容的两个变量,可以使用自动类型转换或强制类型转换。但是,如果两个变量的类型不兼容,比如string与int或string与double,这个时候我们可以使用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
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数