简体   繁体   English

C# 中的 System.FormatException

[英]System.FormatException in C#

I keep getting a FormatException in each of the cases on the line where I try to assign the value for the sale variable.在我尝试为销售变量分配值的行中的每种情况下,我都不断收到 FormatException。 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 aa 10% commission of each sale.它应该根据每次销售的 10% 佣金保留销售人员佣金的运行标签。 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();
      }
   }
}

I keep getting a FormatException in each of the cases on the line where I try to assign the value for the sale variable.在我尝试为销售变量分配值的行中的每种情况下,我都不断收到 FormatException。 Anyone know what I am doing wrong?有谁知道我做错了什么?

The Convert.ToDouble method will raise a FormatException if the string (returned from Console.ReadLine() ) is not a valid number.如果字符串(从Console.ReadLine()返回)不是有效数字,则Convert.ToDouble方法将引发FormatException

Typically, if you want to parse user input, it's a better idea to use Double.TryParse instead, as this lets you determine whether the input was a valid number without catching the exception.通常,如果要解析用户输入,最好改用Double.TryParse ,因为这样可以确定输入是否为有效数字而不会捕获异常。

This would normally look something like:这通常看起来像:

Console.Write("Enter the sales for Andrea >> ");
while (!double.TryParse(Console.ReadLine(), out sale))
{
    Console.WriteLine("Value entered was not a valid number.");
    Console.Write("Enter the sales for Andrea >> ");
}
// Once you get here, "sale" will be set appropriately

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 "". Console.Read 只读取“回车第二部分”并返回“”。 This is why the Convert fails.这就是转换失败的原因。

replace代替

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

with

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

Replace代替

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

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM