博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#中的类型转换,int.parse("ab")为什么会出错
阅读量:7259 次
发布时间:2019-06-29

本文共 2980 字,大约阅读时间需要 9 分钟。

 

一、问题

  

  在一个简单的闰年问题的判断中,输入框获取的字符串,我们希望输入数字字符串,将字符串转换为int整数进行判断。编写程序如下

 

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApplication2{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void textBox1_TextChanged(object sender, EventArgs e)        {        }        private Boolean checkleap(int year)        {            bool temp = false;            if (year % 4 == 0)            {                temp = true;            }            if (year % 100 == 0)            {                temp = false;            }            if (year % 400 == 0)            {                temp = true;            }            return temp;        }        private void Form1_Load(object sender, EventArgs e)        {        }        private void button1_Click(object sender, EventArgs e)        {            String str = textBox1.Text;            String str1 = "";            if (checkleap(int.Parse(str)))            {                str1 = "输入年份为闰年";            }            else            {                str1 = "输入结果不为闰年";            }            MessageBox.Show(str1);        }    }}

     但是当我们输入非法字符时 int.parse("ab")抛出异常。

        为什么int.parse("ab")会抛出异常呢?应该怎么处理?

    我们接下来先讨论c#中的几种类型转换

二、 几种类型转换方式。

 

     1.(int) 是一种类型转换,当我们从int类型到long,float,double,decimal类型,可以使用隐式转换,但是当我们从long类型到int类型就需要使用显式转换,否则会产生编译错误

  2.int.Parse()是一种类容转换;表示将数字内容的字符串转为int类型。  

  如果字符串为空,则抛出ArgumentNullException异常;  

  如果字符串内容不是数字,则抛出FormatException异常;  

  如果字符串内容所表示数字超出int类型可表示的范围,则抛出OverflowException异常;

  3、int.TryParse与 int.Parse 又较为类似,但它不会产生异常,转换成功返回 true,转换失败返回 false。  

  最后一个参数为输出值,如果转换失败,输出值为 0,如果转换成功,输出值为转换后的int值

  4、Convert.ToInt32()是一种类容转换;但它不限于将字符串转为int类型,还可以是其它类型的参数;

  比较:Convert.ToInt32 参数为 null 时,返回 0; int.Parse 参数为 null 时,抛出异常。Convert.ToInt32 参数为 "" 时,抛出异常; int.Parse 参数为 "" 时,抛出异常。   Convert.ToInt32 可以转换的类型较多; int.Parse 只能转换数字类型的字符串

 

三、问题分析

  由于输入字符串ab不是数字因此跑出了“FormatException”。

  在类型转换讨论中,我们提到了int.TryParse,或许可以解决问题。

  修改后的关键代码如下:

  

 

1        private void button1_Click(object sender, EventArgs e) 2         { 3             String str = textBox1.Text; 4             String str1 = ""; 5             int num = 0; 6             bool temp = int.TryParse(str, out num); 7             if (temp) 8             { 9                 if (checkleap(num))     10                     str1 = "输入年份为闰年";11                 else12                     str1 = "输入结果不为闰年";13             }14             else15             {16                 str1 = "输入年份格式不正确";17             }18             MessageBox.Show(str1);19         }

  测试用例:

测试用例 预计输出 实际输出
“1234” 输入结果不为闰年 输入结果不为闰年
“2000” 输入结果为闰年 输入结果为闰年
“1900” 输入结果不为闰年   输入结果不为闰年
“abcd” 输入年份格式不正确 输入年份格式不正确
“” 输入年份格式不正确 输入年份格式不正确

  测试截图:

因此我们在以后的编程中,要注重int.Tryparse()的使用,以及对各种情况的考虑。

 

转载于:https://www.cnblogs.com/zzy-blogs/p/4398731.html

你可能感兴趣的文章
py django 引入 wiki 模块
查看>>
Logic-算法-分金条
查看>>
页面跳转参数不丢失
查看>>
对于 飞林沙的<把Array说透>的扩展
查看>>
使用shell脚本生成只读权限的sql脚本
查看>>
Add SSH Key to GitLab on Windows
查看>>
浙大复试(二)
查看>>
js深入研究之匿名函数
查看>>
Enabling the Dedicated Administrator Connection (DAC) in SQL Server Express
查看>>
[推荐]前端性能分析工具:dynaTrace Ajax Edition
查看>>
泛型算法
查看>>
zabbix监控主机cpu达到80%后报警
查看>>
4.15. gulp-sourcemaps
查看>>
C#实现文件数据库
查看>>
测试用例介绍
查看>>
赛先生:烧,烧,烧,每年70万亩
查看>>
精彩批处理代码
查看>>
2.5. 流量控制
查看>>
编写更好的C#代码
查看>>
new & override 不完全PK
查看>>