Amy_is_growing 2017-07-17 01:26 采纳率: 66.7%
浏览 827
已采纳

C#代码中出现的小问题,小白求指教

        #region 计算最大值
        Console.WriteLine("请输入两个字符:");
        int n1 = Convert.ToInt16 (Console.ReadLine());
        int n2 = Convert.ToInt16(Console.ReadLine());      
        int max = GetMaxNumbera(n1, n2);
        Console.WriteLine("最大值是:{0}", max);
        Console.ReadKey();
        #endregion
                    private static int GetMaxNumbera(int n1, int n2)
    {
        return (n1 > n2) ? n1 : n2;
    }
            出错:
            ![图片说明](https://img-ask.csdn.net/upload/201707/17/1500254749_341589.png)
  • 写回答

1条回答 默认 最新

  • Go 旅城通票 2017-07-17 02:11
    关注

    你输入的不是数字吧。。不是数字Convert.ToInt16肯定转换错了。。

    
                Console.WriteLine("请输入两个字符:");
                int n1 =0;
                int.TryParse(Console.ReadLine(), out n1);//用TryParse,输入转换不了的数字也不会提示错误,n2同理
                int n2 = 0;
                int.TryParse(Console.ReadLine(), out n2);
                int max = GetMaxNumbera(n1, n2);
                Console.WriteLine("最大值是:{0}", max);
                Console.ReadKey();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 overleaf中论文编辑,报错`pages' is a missing field, not a string, for entry 4
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么