System.FormatException在C# [英] System.FormatException in C#

查看:269
本文介绍了System.FormatException在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到在每个上线的情况下,我尝试分配为销售变量的值出现FormatException。任何人都知道我在做什么错了?我应该让这个控制台程序功课,了解循环,但我发现更多关于其他的事情。它应该保持基于10%的佣金每笔销售的销售人员的佣金的运行选项卡。不管怎么说,这里是代码:

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;

命名空间TubSales
{
类节目
{
静态无效的主要(字串[] args)
{
焦炭初始;
常量双COMM_INT = 0.10;
双销售,aComm = 0,bComm = 0,逸康= 0;
Console.Write(输入A安德烈,'B'的布列塔尼,\\\
'E埃里克或'Z'退出>>);
初始= Convert.ToChar(Console.Read());
,而(初始='Z'和;&安培;初始='Z'!)
{
开关(初始)
{
案一:
案'A':
Console.Write(输入安德烈>的销量;>中);
=销售Convert.ToDouble(到Console.ReadLine());
aComm = aComm + COMM_INT *销售;
中断;
案'B':
案'B':
Console.Write(输入布列塔尼>的销量;>中);
=销售Convert.ToDouble(到Console.ReadLine());
bComm = bComm + COMM_INT *销售;
中断;
案'E':
案'E':
Console.Write(输入埃里克>的销量;>中);
=销售Convert.ToDouble(到Console.ReadLine());
逸康=逸康+ COMM_INT *销售;
中断;
默认:
Console.WriteLine(你没有输入一个有效的初始);
中断;
}
Console.Write(输入A安德烈,'B'的布列塔尼,或'E'埃里克>>);
初始=(char)的Console.Read();
}
Console.WriteLine(安德烈{0},布列塔尼有{1},和Eric有{2}的好处费。aComm.ToString(C),bComm.ToString( C),eComm.ToString(C));
Console.Write(按任意键退出...);
Console.ReadKey();
}
}
}


解决方案

尽管里德的答案是伟大的,它不是这里的问题。



真正发生的是同样的情况的这个



<仅p> Console.Read阅读和返回后回车的第二部分。这就是为什么转换失败。



替换

 初始=转换.ToChar(Console.Read()); 



 初始= Convert.ToChar(到Console.ReadLine()); 


I keep getting a FormatException in each of the cases on the line where I try to assign the value for the sale variable. Anyone know what I am doing wrong? I am supposed to make this console program as homework to learn about loops, but I am finding out more about other things. It is supposed to keep a running tab of salesperson's commission based a a 10% commission of each sale. Anyways, here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TubSales
{
   class Program
   {
      static void Main(string[] args)
      {
         char initial;
         const double COMM_INT = 0.10;
         double sale, aComm = 0, bComm = 0, eComm = 0;
         Console.Write("Enter 'A' for Andrea, 'B' for Brittany,\n'E' for Eric, or 'Z' to quit >> ");
         initial = Convert.ToChar(Console.Read());
         while (initial != 'z' && initial != 'Z')
         {
            switch (initial)
            {
               case 'a':
               case 'A':
                  Console.Write("Enter the sales for Andrea >> ");
                  sale = Convert.ToDouble(Console.ReadLine());
                  aComm = aComm + COMM_INT * sale;
                  break;
               case 'b':
               case 'B':
                  Console.Write("Enter the sales for Brittany >> ");
                  sale = Convert.ToDouble(Console.ReadLine());
                  bComm = bComm + COMM_INT * sale;
                  break;
               case 'e':
               case 'E':
                  Console.Write("Enter the sales for Eric >> ");
                  sale = Convert.ToDouble(Console.ReadLine());
                  eComm = eComm + COMM_INT * sale;
                  break;
               default:
                  Console.WriteLine("You did not enter a valid initial");
                  break;
            }
            Console.Write("Enter 'A' for Andrea, 'B' for Brittany, or 'E' for Eric >> ");
            initial = (char)Console.Read();
         }
         Console.WriteLine("Andrea had {0}, Brittany had {1}, and Eric had {2} in commissions.", aComm.ToString("C"), bComm.ToString("C"), eComm.ToString("C"));
         Console.Write("Press any key to exit... ");
         Console.ReadKey();
      }
   }
}

解决方案

While Reed's answers is great, it's not the problem here.

What really happens is the same situation as this one

Console.Read only read the "Second part of the carriage return" and returns "". This is why the Convert fails.

replace

initial = Convert.ToChar(Console.Read());

with

initial = Convert.ToChar(Console.ReadLine());

这篇关于System.FormatException在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆