【问题标题】:Please help me debug my calculator program [closed]请帮我调试我的计算器程序[关闭]
【发布时间】:2011-08-18 22:07:37
【问题描述】:

我正在尝试制作一个计算器。每当我输入 25 * 4 / 10 之类的东西时,它会将 25 除以 4。这是我认为可能是问题的代码部分:

    private void button16_Click(object sender, RoutedEventArgs e)
    {

        string[] calCulation = CALCULATION.Text.Split('-', '+', '/', 'X');
        int numOfItems = calCulation.Length;
        int count = 1;
        char[] Arius = new char[Hey.Length];
        foreach(char words in Hey)
        {
            int outlie = 0;
            Arius[outlie] = words;
            outlie++;
        }
        decimal final = 0M;
        decimal[] calCulate = new decimal[numOfItems];
        int countfreak = 0;
        foreach (string word in calCulation)
        {

            calCulate[countfreak] = Convert.ToDecimal(word);
            countfreak++;
        }
        int counting = 1;
        int countinghey = 0;
        decimal final2 = calCulate[0];

        while(count < numOfItems){


            switch(Arius[countinghey])
            {
                case 'X':
                    /*
                        final2 += final * calCulate[counting -1];
                        final2 = final2 * calCulate[counting];
                     */
                    final2 = final2 * calCulate[counting];
                    break;
                case '-':


                        final2 = final2 - calCulate[counting];
                    break;
                case '+':


                        final2 = final2 + calCulate[counting];
                    break;
                case '/':


                        final2 = final2 / calCulate[counting];
                    break;
            }
             counting++;
             countinghey++;
             count++;

        }
        CALCULATION.Text = Convert.ToString(final2);
    }
    public bool Parshing(string value, string typee)
    {

        int hixty = value.Length;
        string six = value.Substring(hixty - 1, value.Length - hixty + 1);
        int lam;
        bool result = Int32.TryParse(six, out lam);
        if (result == true||six == "")
        {
            CALCULATION.Text += typee;
            Hey += typee;
        }
        else
        {
        }

        return result;
    }

【问题讨论】:

  • 添加一些断点并自己调试。找到这个问题的答案的最佳方式。
  • button16,嘿,final2,freak,num vs cal 没有任何可读性分数。这些事情很重要。很多。因为突然你不能再调试代码了。

标签: c# wpf arrays loops calculator


【解决方案1】:

马上,你没有正确分裂。

string[] calCulation = CALCULATION.Text.Split('-', '+', '/', 'X');

假设您输入了:

 25 * 4 / 10

您应该将拆分从“X”更改为“*”

string[] calCulation = CALCULATION.Text.Split('-', '+', '/', '*');

您还需要更改您的案例陈述。或者确保您的输入是正确的。

【讨论】:

  • nooo 实际上我输入 25X4/10 哎呀我搞砸了
猜你喜欢
  • 1970-01-01
  • 2022-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
  • 2019-09-13
相关资源
最近更新 更多