【问题标题】:Additional information: Input string was not in a correct format附加信息:输入字符串的格式不正确
【发布时间】:2015-08-05 22:21:31
【问题描述】:

我正在尝试让这个程序运行,但没有骰子。我认为它很完美,但缺少一些东西。它应该根据门票的数量和购买的门票类型显示门票的价格,然后显示所有门票的价格和总收入。

namespace stadiumApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label7_Click(object sender, EventArgs e)
        {
        }

        private void calculateRevenue_Click(object sender, EventArgs e)
        {
           //assigns input to saved memory 
                string inputA="";
                string inputB ="";
                string inputC ="";

                double ticketsPurchasedB = double.Parse(inputB);
                double ticketsPurchasedA = double.Parse(inputA);
                double ticketsPurchasedC = double.Parse(inputC);

               //price of each ticket grade
                double priceA = 15;
                double priceB = 12;
                double priceC = 9;

                //calculations for each class
                double calcOutputA = priceA * ticketsPurchasedA ;
                string outputA = calcOutputA.ToString();

            double calcOutputB = priceB * ticketsPurchasedB;
            string outputB = calcOutputB.ToString();

            double calcOutputC = priceC * ticketsPurchasedC;
            string outputC = calcOutputC.ToString();

           //calculations for total revenue
         double totalLabelHolder = calcOutputA + calcOutputB + calcOutputC;
           //display total 
            string totalLabel = totalLabelHolder.ToString();
      }
   }
}

【问题讨论】:

  • 这个问题陈述不是很具体。请说明您遇到了什么错误,究竟是什么不起作用,等等。
  • 通过一些代码,我们只能说“是的,按书面方式工作”。将其简化为一个最小的示例,说明您想要/期望什么、您得到什么以及您尝试过什么。

标签: c# winforms exception input


【解决方案1】:

您需要在表单上创建文本框并使用它们的值来代替:

string inputA="";
string inputB ="";
string inputC ="";

这些行中的每一行都会抛出一个错误(因为你不能将空字符串解析为双精度):

double ticketsPurchasedB = double.Parse(inputB);
double ticketsPurchasedA = double.Parse(inputA);
double ticketsPurchasedC = double.Parse(inputC);

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2013-08-22
    相关资源
    最近更新 更多