我是新手C#編碼人員,目前正在研究反向波蘭符號計算器。我創建了一個方法來處理計算,但是在我的代碼中有三行導致錯誤。在每個=
之後執行操作然後顯示。我試圖從TxtInputBox
中獲取字符串並將其轉換爲整數,但下面這行是我得到紅色波浪線的地方:string[] inputarray = TxtBoxInputbox.Text.Split();
。第二次調用函數RPNCalc從按鈕單擊Btn_Calc
並分配文本框時,我會收到另一條紅色波浪線:RPNCalc(TxtInputBox, TxtOutputBox);
我不知道如何處理這兩個使我的表單無法工作的錯誤。請幫助使錯誤免費。反向波蘭語表示法:從輸入文本框中獲取值以執行計算
代碼
namespace rpncalc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void RPNCalc(TxtBoxInputbox, TxtBoxOutputbox)
{
Stack<int> stackone = new Stack<int>();
stackone.Clear();
string[] inputarray = TxtBoxInputbox.Text.Split();
int end = inputarray.Length - 1;
int numinput;
int i = 0;
do
{
if(inputarray[i] != "=" && inputarray[i] != "+" && inputarray[i] != "-" && inputarray[i] != "*" && inputarray[i] != "/")
{
try
{
numinput = Convert.ToInt32(inputarray[i]);
stackone.Push(numinput);
}
catch
{
MessageBox.Show("Please check the input");
}
}
else if (inputarray[i]== "+")
{
try
{
int store1 = stackone.Pop();
int store2 = stackone.Pop();
stackone.Push(store2 + store1);
}
catch
{
}
}
else if (inputarray[i]== "-")
{
try
{
int store1 = stackone.Pop();
int store2 = stackone.Pop();
stackone.Push(store2 + store1);
}
catch
{
}
}
else if (inputarray[i]== "+")
{
try
{
int store1 = stackone.Pop();
int store2 = stackone.Pop();
stackone.Push(store2 + store1);
}
catch
{
}
}
else if (inputarray[i]== "*")
{
try
{
int store1 = stackone.Pop();
int store2 = stackone.Pop();
stackone.Push(store2 + store1);
}
catch
{
}
}
else if (inputarray[i]== "/")
{
try
{
int store1 = stackone.Pop();
int store2 = stackone.Pop();
stackone.Push(store2 + store1);
}
catch
{
}
}
}
while(i < end && inputarray[i]!= "=" && stackone.Count != 0);
string txtout = TxtInputBox + " " + stackone.Pop().ToString() + Environment.NewLine;
TxtOutputBox.AppendText(txtout);
TxtInputBox.Clear();
}
private void Btn_Calc_Click(object sender, EventArgs e)
{
RPNCalc(TxtInputBox, TxtOutputBox);
}
}
}
Squiggly行很好,但實際*錯誤*是什麼? (如果您使用VS,請查看錯誤列表。)閱讀此內容(通常)會有所幫助。 –