错误代码如下:
using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入一个浮点型数据:");
            int num = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine("浮点型数据转换成整型后的数据为" + num);
            Console.ReadLine();
        }
    }
}
疑惑:
为什么我输入浮点型数据就报错“输入格式不正确”
当输入整型数据时就能正常运行了呢?
请教解答

解决方案 »

  1.   

    ToInt16和ToInt32等函数只接受整型内容,也就是说你传10进去OK,传10.0进去报错
    所以你要先转成double或者decimal,再转成整型
      

  2.   

    decimal num =0;
     decimal.TryParse(Console.ReadLine(),out num);
      

  3.   

    你既然是要输入浮点数据(float|decimal)
    而你前面去输入却神秘int 整型,你说出错在哪里?
      

  4.   

                float num = float.Parse(Console .ReadLine());
      

  5.   


    对于楼主将浮点型转换为int型,并问错在哪里的行为,个人比较晕
      

  6.   

    ToInt16和ToInt32等函数只接受整型内容,也就是说你传10进去没问题,传10.0进去报错,
    所以你要先转成double或者decimal,再转成整型
    decimal num =0;
    decimal.TryParse(Console.ReadLine(),out num);
      

  7.   

    int num = Convert.ToInt16(Console.ReadLine());
    num是int 型的 当然输入 浮点型的会报错了
      

  8.   

    decimal num =0;
    decimal.TryParse(Console.ReadLine(),out num);
      

  9.   

    Int32的意思就是整数啊,你换成double就可以了
      

  10.   

    float num=float.parsefloat(System.ReadLine());
      

  11.   

    int num = Convert.Todouble(Console.ReadLine());
      

  12.   

    LZ明显是想知道 "浮点型数据转换成整型后的数据为" ,要转成int很正常啊,还有convert.toint32参数可以是float啊,如果像1楼说的 "ToInt16和ToInt32等函数只接受整型内容" 那我何必要转呢, 还有Console.ReadLine()是string型的,不是float型的.所以,我认为convert.toint32(10.0)是可以的,convert.toint32("10")也是可以的,但是convert.toint32("10.0")转换时,直接把string转成int型,此时10.0不符合int格式,所以不可以