zoukankan      html  css  js  c++  java
  • 当当当当 int.Parse()异常~

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string inputNum;
                int num;
                Console.WriteLine("Please enter your input number");
                inputNum = Console.ReadLine();
                num = int.Parse(inputNum);
    
                Console.WriteLine("Your input is" + inputNum);
                Console.WriteLine("After num" + num);
    
               
            }
        }
    }

    当输入字符非数字时,会出现下列情况

    我们应该用什么方法来解决它呢?

    下列几个方法是可以使用的

    1.使用try,catch语句,将源代码改成下列代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string inputNum;
                int num;
                Console.WriteLine("Please enter your input number");
                inputNum = Console.ReadLine();
    
                try
                {
                    num = int.Parse(inputNum);
                }
                catch (Exception)
                {
                    Console.WriteLine("Your input is wrong!!!");
                }
    
            
               
            }
        }
    }

    这样通过使用try, catch我们可以接到抛出的异常,结果如图

    2. 使用int.TryParse (String s,out int num.)与 int.Parse(string s)又较为类似,但它不会产生异常,最后一个参数为输出值,如果转换失败,输出值为 0,如果转换成功,输出值为转换后的int值。

    代码行如下

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string inputNum;
                int num;
                
                Console.WriteLine("Please enter your input number");
                inputNum = Console.ReadLine();
               
    
                Console.WriteLine(int.TryParse(inputNum, out num));
                Console.WriteLine(num);
    
    
            }
        }
    }

    测试结果如下

    附:另外两种string转换成int的方法

    1. Convert.ToInt32()是一种类容转换;但它不限于将字符串转为int类型,还可以是其它类型的参数;Convert.ToInt32 与 int.Parse 较为类似,实际上 Convert.ToInt32 内部调用了 int.Parse。

    2. (int) 属 cast 转换,当我们把int类型扩展到long,float,double,decimal类型,可以使用隐式转换,但是当我们从long类型到int类型就需 要使用显式转换,否则会产生编译错误。但我们只能将其它数字类型转换成 int 类型,它不能转换字符串,比如下例就会失败:   string v = "1";   int n = (int)v;

    THX 4 ur reading~

    ^ ^

  • 相关阅读:
    Cdnbest的cdn程序默认支持web Socket
    在winsshd 中添加id_rsa.pub 实现Windows 服务器主机自动信任Linux 客户端
    Excel 如何排序与多关键字排序
    JZOJ 6316. djq的朋友圈(状压DP)
    JZOJ 6309. 完全背包(矩阵max)
    JZOJ 6307. 安排(归并排序+分治)
    JZOJ 6299. 2019.08.12【NOIP提高组A】工厂(二分图+状压DP)
    JZOJ 6276. 【noip提高组模拟1】树(DFS序+扫描线)
    JZOJ 6278. 2019.8.5【NOIP提高组A】跳房子 (分块模拟)
    JZOJ 6278. 2019.8.5【NOIP提高组A】跳房子 (分块模拟)
  • 原文地址:https://www.cnblogs.com/acrophobia/p/4400938.html
Copyright © 2011-2022 走看看