C# 当我在eg'*';如果出现错误,请随意编辑代码
操作错误单击两个操作时,我希望进行错误处理,因此我无法将多个*放在一行中C# 当我在eg'*';如果出现错误,请随意编辑代码,c#,calculator,calc,C#,Calculator,Calc,操作错误单击两个操作时,我希望进行错误处理,因此我无法将多个*放在一行中 namespace Calculator_Assignment { public partial class Form1 : Form { public Form1() { InitializeComponent(); } float num, ans; int count; priv
namespace Calculator_Assignment
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
float num, ans;
int count;
private void Form1_Load(object sender, EventArgs e)
{
}
private void Button_click(object sender, EventArgs e)
{
Button button = (Button)sender; //adds the numbers into the screen when clicked
Screen.Text = Screen.Text + button.Text;
Screen.ForeColor = Color.Red; //text that entered appears red
}
private void operatorclick(object sender, EventArgs e)
{
Button button = (Button)sender; //adds the symbols into the screen when clicked
Screen.Text = Screen.Text + button.Text; //all symbols are under button_click so i do not have to repeat the code over/
}
private void Clearclick(object sender, EventArgs e)
{
Screen.Clear(); //when clicked clears the screen
Result.Clear(); //when clicked clears the result
}
private void Decimalclick(object sender, EventArgs e)
{
Screen.Text = Screen.Text + "."; //adds decimal point to screen when/if clicked
Screen.ForeColor = Color.Red; //decimal point appears red
}
private void Closebtn_Click(object sender, EventArgs e)
{
this.Close(); // closes the application down
}
private void plusclick(object sender, EventArgs e) //addition
{
num = float.Parse(Screen.Text);
Screen.Clear(); //clears the screen of everything
Screen.Focus(); //textbox is focused upon when the screen is cleared
count = 1; //this counts the store case
Screen.Text = num.ToString() + "+"; //this puts the text onto the top text box
}
private void multiplyclick(object sender, EventArgs e) //multiply
{
num = float.Parse(Screen.Text);
Screen.Clear(); //clears the screen of everything
Screen.Focus(); //textbox is focused upon when the screen is cleared
count = 3; //this counts the store case
Result.Text = num.ToString() + "*"; //this puts the text onto the top textbox
}
private void divideclick(object sender, EventArgs e) //divide
{
num = float.Parse(Screen.Text);
Screen.Clear(); //clears the screen of everything
Screen.Focus(); //textbox is focused upon when the screen is cleared
count = 4; //this counts the store case
Screen.Text = num.ToString() + "/"; //this puts the text onto the
}
private void subtractclick(object sender, EventArgs e) //subtract
{
num = float.Parse(Screen.Text);
Screen.Clear(); //clears the screen of everything
Screen.Focus(); //textbox is focused upon when the screen is cleared
count = 2; //this counts the store case
Result.Text = num.ToString() + "-"; //this puts the text onto the label
}
private void equalsclick(object sender, EventArgs e)
{
switch (count) //initalising switch statement
{
case 1:
ans = num + float.Parse(Screen.Text);//Adding numbers
Result.Text = ans.ToString(); //this converts my answer from a float to a string
break;
case 2:
ans = num - float.Parse(Screen.Text); //Subtracting numbers
Result.Text = ans.ToString(); //float to a string
break;
case 3:
ans = num * float.Parse(Screen.Text); //Multiplying numbers
Result.Text = ans.ToString(); //float to a string
break;
case 4:
ans = num / float.Parse(Screen.Text); //Division of numbers
Result.Text = ans.ToString(); //float to a string
break;
default: //the default figure
break;
}
}
}
}
快速解决方案:
创建一个名为void AppendOperator(字符串运算符)
此方法应在追加前一个字符之前检查它。如果前一个字符是运算符,则只需返回
调用此新方法,以替换为Result.Text赋值的所有位置
附加运算符实现示例:
private readonly HashSet<char> _operators = new HashSet<char>() { '+', '*', '-', '/' };
void AppendOperator(string operation)
{
char lastChar = Result.Text.LastOrDefault();
if (_operators.Contains(lastChar)) return;
Result.Text = num.ToString() + operation;
}
private readonly HashSet_operators=new HashSet(){'+','*','-','/'};
无效附加运算符(字符串操作)
{
char lastChar=Result.Text.LastOrDefault();
if(_operators.Contains(lastChar))返回;
Result.Text=num.ToString()+操作;
}
brilliant谢谢你,方法代码实际上是什么?一分钟(不是在笔记本电脑前)回答了你的问题吗?还有什么我可以帮忙的吗?