这是整个代码:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Calculator
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
richTextBox1.ReadOnly = true;
}
//inputs
void Button1Click(object sender, EventArgs e)
{
richTextBox1.AppendText("1");
}
void Button2Click(object sender, EventArgs e)
{
richTextBox1.AppendText("2");
}
void Button3Click(object sender, EventArgs e)
{
richTextBox1.AppendText("3");
}
void Button6Click(object sender, EventArgs e)
{
richTextBox1.AppendText("4");
}
void Button5Click(object sender, EventArgs e)
{
richTextBox1.AppendText("5");
}
void Button4Click(object sender, EventArgs e)
{
richTextBox1.AppendText("6");
}
void Button9Click(object sender, EventArgs e)
{
richTextBox1.AppendText("7");
}
void Button8Click(object sender, EventArgs e)
{
richTextBox1.AppendText("8");
}
void Button7Click(object sender, EventArgs e)
{
richTextBox1.AppendText("9");
}
void Button10Click(object sender, EventArgs e)
{
richTextBox1.AppendText("0");
}
void Button14Click(object sender, EventArgs e)
{
richTextBox1.AppendText("+");
}
void Button13Click(object sender, EventArgs e)
{
richTextBox1.AppendText("-");
}
void Button12Click(object sender, EventArgs e)
{
richTextBox1.AppendText("X");
}
void Button11Click(object sender, EventArgs e)
{
richTextBox1.AppendText("/");
}
void Button15Click(object sender, EventArgs e)
{
String fetch = richTextBox1.Text;
String convert = fetch.Replace('X', '*');
try
{
int equ = Int32.Parse(convert);
}
catch (FormatException exe)
{
MessageBox.Show(exe.Message);
}
}
}
}
这是引发异常的部分:
void Button15Click(object sender, EventArgs e)
{
String fetch = richTextBox1.Text;
String convert = fetch.Replace('X', '*');
try
{
int equ = Int32.Parse(convert);
}
catch (FormatException exe)
{
MessageBox.Show(exe.Message);
}
}
由于某种原因,Int32.parse无法将“转换”字符串更改为整数,而是抛出“输入字符串格式不正确”。我不知道为什么。我也尝试过使用Convert.Int32(),但是它仍然会引发相同的异常。