System.FormatException错误以及尝试修复该问题

时间:2018-10-04 22:22:14

标签: c# console-application

您好,我的项目目前有问题。我的控制台程序目前存在问题,我正在接受用户输入(其中是小数),然后在if els语句中使用它们,然后最后做最后的数学运算来计算出机票费用。

我一直在研究可以解决此问题的方法,但是在过去的几个小时中,我一直找不到解决方法。

我曾尝试使用字符串,inter,var和boolen来存储价格,但是当涉及到最终数学计算费用时,只有inter不会给我错误。

我认为一种解决方法是改变用户选择他们想要的机票的方式,但是我无法找到一种方法,允许他们从机票菜单中选择,同时将价格值分配给他们的输入说: / p>

Int family = 39.90

然后以某种方式使用它来使用户输入基于我所声明的值。

请有人能建议我可以做些不同的事情还是解决我目前的除法/ FormatException错误的方法吗?

在格式方面的任何其他技巧也将不胜感激,欢迎大家批评并尝试学习。

当前控制台代码:

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

 namespace The_Admission_Price_Calculator
 {
 class Program
 {


    static void Main(string[] args)
    {
        Action<string> cw = Console.WriteLine;

        cw("Hello welcome to the WildLife park");
        cw("We currently have 4 ticket options:");
        cw("1: Famliy ticket(2 adults + 3 children) £39.90"); 
        cw("2: Adult ticket £14.90");
        cw("3: Child (under 16) £9.90");
        cw("4: Senior (over 65) £7.00");

        cw("Please input the price of the ticket you would like?");
        cw("(EG if you want to child ticket please input 9.90, please also include the decimal place.");
        cw("Input your the tickets price you would like to order now please.");

        string Answer1;
        int TicketCost1;
        int TicketAmount1;
        int TicketType1;



        TicketType1 = Convert.ToInt32(Console.ReadLine());

        if  (TicketType1 == 39.90)
                {
                //int Famliy = 3990;   
                }

             else if (TicketType1 == 9.90)
                    {
                    //int Child = 990;
                    }

                        else if (TicketType1 == 14.90)
                        {
                        //int Adult = 1490;
                        }

                            else if (TicketType1 == 7.00)
                            {
                            //int Senior = 700;
                            }
                                else
                                {
                                Console.WriteLine("you need to Input from the options, using the price of the ticket, with the decimal included.");
                                TicketType1 = Convert.ToInt32(Console.ReadLine());
        }



        cw("your choosen ticket is " + TicketType1 + ", how many tickets of this type would you like?");
        TicketAmount1 = int.Parse(Console.ReadLine());

        //Rember to Add /100 to the final sum, so that the output is in decimals.  


        TicketCost1 = TicketAmount1 * TicketType1;




        cw("With the choosen amount of tickets of " +TicketAmount1+ " this will cost £" +TicketCost1+" ");

        cw("Is this correct? (YES OR NO");
        Answer1 = Console.ReadLine();
        if (Answer1 == "YES")
        {
            cw("Tickets are printing now.");
        }

        if (Answer1 == "NO")
        {
            cw("Please reselect what tickets you would like");
            //code here
        }

        else
        {
            cw("You have not entred a vaild asnswer please Input YES Or not in captials");
            Answer1 = Console.ReadLine();
            //core here
        }



        Console.ReadKey();

    }

}

}

2 个答案:

答案 0 :(得分:0)

您不能将小数放入int中。整数保存整数。传统上,您使用double来存储小数,但对于货币而言,这是个坏习惯。对于货币,请使用十进制类型http://csharpindepth.com/Articles/General/Decimal.aspx

答案 1 :(得分:0)

我要更改的一件事是,当用户从菜单中进行选择时,让用户输入选项的编号而不是价格,这应该会使您的事情变得容易一些(然后选择是int的)

就像每个人的建议一样,从使用int转换为十进制。

这也会影响您从字符串输入进行的转换:

//TicketType1 = Convert.ToInt32(Console.ReadLine());   
TicketType1 = Convert.ToDecimal(Console.ReadLine());

它也会影响您进行比较的方式:

// if (TicketType1 == 39.90)
   if (Decimal.Compare(TicketType1,39.90) == 0)