我有一个非常烦人的问题,我一直在寻找无处不在找,但没有一个对我有意义。我最近才刚刚开始C#,所以如果它是一个愚蠢的错误,非常抱歉。计算器中的输入字符串格式不正确
我建立了一个计算器,我可以成功地做到这一点,但我希望它显示的操作,当用户点击它们。例如,当用户点击6按钮的时候,当他按下加号(+)按钮时,它在文本框然后中显示6,它应该显示[6 +],然后他按5例如,它看起来像这样在文本框[6 + 5]中。
现在,这是我的错误。我可以做所有上述工作,但是当我点击等号(=)按钮时,我得到一个错误。它说
“输入字符串格式不正确。”
它说的错误是这行代码:
decimal total = Convert.ToDecimal(LCD.Tag) +
Convert.ToDecimal(LCD.Text);
继承人我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Window : Form
{
bool pluss = false;
bool minuss = false;
bool multiplyy = false;
bool dividee = false;
public Window()
{
InitializeComponent();
}
private void clear_Click(object sender, EventArgs e)
{
LCD.Text = "";
}
private void dec_Click(object sender, EventArgs e)
{
if (LCD.Text.Contains("."))
{
return;
}
else {
LCD.Text = LCD.Text + ".";
}
}
private void zero_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "0";
}
private void one_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "1";
}
private void two_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "2";
}
private void three_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "3";
}
private void four_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "4";
}
private void five_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "5";
}
private void six_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "6";
}
private void seven_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "7";
}
private void eight_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "8";
}
private void nine_Click(object sender, EventArgs e)
{
LCD.Text = LCD.Text + "9";
}
private void plus_Click(object sender, EventArgs e)
{
if (LCD.Text == "")
{
return;
}else{
pluss = true;
LCD.Tag = LCD.Text;
LCD.Text = LCD.Text + " + ";
}
}
private void equal_Click(object sender, EventArgs e)
{
decimal total = Convert.ToDecimal(LCD.Tag) + Convert.ToDecimal(LCD.Text);
LCD.Text = total.ToString();
}
}
}
我在等待某人的回应,我会如此感激,如果我得到一个修复。 谢谢。
如果LCD.Text包含LCD.Text +“+”。它会失败! – c0deNinja 2012-07-10 22:32:53
@Ed S. Convert.ToDecimal(“4 + 2”)将失败 – c0deNinja 2012-07-10 22:35:55
@ c0deNinja:不,确实没有。也许你应该测试一下。现在,没有找到有效数字的单个“+”或多个数字(即“4 + 2”)当然会失败。 – 2012-07-10 22:36:29