计算器问题 [英] calculator problem
本文介绍了计算器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是C#的新手,我在使用这个计算器代码时遇到了困难。一切都有效,直到你尝试用计算结果做某事。例如,如果我做8 * 2并得到16,我就不能从这个结果中加/减/乘/除。这是代码
。
名称空间计算器
{
  ;  公共部门类Form1:表格
  {
      string input = string.Empty;
      string operand1 = string.Empty;
      string operand2 = string.Empty;
      char操作;
      double result = 0.0;
      public Form1()
      {
          InitializeComponent();
      }
      private void Zero_Click(object sender,EventArgs e)
      {
          if(textBox1.Text =="")
          {
              this.textBox1.Text ="" ;;
             输入+ =" 0";
              this.textBox1.Text + =输入;
          }¥b $ b          否则if(textBox1.Text ==" 0")
          {
$
          }
      }
      private void One_Click(object sender,EventArgs e)
      {
          this.textBox1.Text ="" ;;
         输入+ =" 1" ;;
          this.textBox1.Text + =输入;
      }
      private void Two_Click(object sender,EventArgs e)
      {
          this.textBox1.Text ="" ;;
         输入+ =" 2" ;;
          this.textBox1.Text + =输入;
      }
      private void Three_Click(object sender,EventArgs e)
      {
          this.textBox1.Text ="" ;;
         输入+ =" 3";
          this.textBox1.Text + =输入;
      }
      private void Four_Click(object sender,EventArgs e)
      {
          this.textBox1.Text ="" ;;
         输入+ =" 4";
          this.textBox1.Text + =输入;
      }
      private void Five_Click(object sender,EventArgs e)
      {
          this.textBox1.Text ="" ;;
         输入+ =" 5"; $
          this.textBox1.Text + =输入;
      }
      private void Six_Click(object sender,EventArgs e)
      {
          this.textBox1.Text ="" ;;
         输入+ =" 6";
          this.textBox1.Text + =输入;
      }
      private void Seven_Click(object sender,EventArgs e)
      {
          this.textBox1.Text ="" ;;
         输入+ =" 7";
          this.textBox1.Text + =输入;
      }
      private void Eight_Click(object sender,EventArgs e)
      {
          this.textBox1.Text ="" ;;
         输入+ =" 8"; $
          this.textBox1.Text + =输入;
      }
      private void Nine_Click(object sender,EventArgs e)
      {
          this.textBox1.Text ="" ;;
         输入+ =" 9"; $
          this.textBox1.Text + =输入;
      }
      private void Decimal_Click(object sender,EventArgs e)
      {
          this.textBox1.Text ="" ;;
         输入+ ="。" ;;
          this.textBox1.Text + =输入;
      }
      private void Divide_Click(object sender,EventArgs e)
      {
          operand1 =输入;
          operation ='/';
          input = string.Empty;
      }
      private void Multiply_Click(object sender,EventArgs e)
      {
          operand1 =输入;
          operation ='*';
          input = string.Empty;
      }
      private void Plus_Click(object sender,EventArgs e)
      {
          operand1 =输入;
          operation ='+';
          input = string.Empty;
      }
      private void Minus_Click(object sender,EventArgs e)
      {
          operand1 =输入;
          operation =' - ';
          input = string.Empty;
      }
      private void Clear_Click(object sender,EventArgs e)
      {
          this.textBox1.Text ="" ;;
          this.input = string.Empty;
          this.operand1 = string.Empty;
          this.operand2 = string.Empty;
      }
      private void Equal_Click(object sender,EventArgs e)
      {
          operand2 =输入;
          double.TryParse(operand1,out double num1);
          double.TryParse(operand2,out double num2);
          this.textBox1.Text ="" ;;
          this.input = string.Empty;
          this.operand1 = string.Empty;
          this.operand2 = string.Empty;
       
          if(operation =='/')
          {
             结果= num1 / num2;
              textBox1.Text = result.ToString();
          }¥b $ b          否则如果(操作=='*')
          {
             结果= num1 * num2;
              textBox1.Text = result.ToString();
          }¥b $ b          否则如果(操作=='+')
          {
             结果= num1 + num2;
              textBox1.Text = result.ToString();
          }¥b $ b          否则如果(操作==' - ')
          {
              if(num2!= 0)
              {
                 结果= num1 / num2;
                  textBox1.Text = result.ToString();
              }¥b $ b              否则
              {
                  textBox1.Text ="错误:不能除以0";
              }¥b $ b           }¥b $ b       }¥b $ b   }
}
Conner Ferguson
解决方案
在 Equal_Click 中获得结果后,也许你应该执行
'input = result.ToString()'。 跨度>
I'm new to C# and I'm having difficulties with this calculator code. Everything works find until you try to do something with the result of the calculation. For example if i do 8*2 and get 16, i cant add/subtract/multiply/divide from this result. Here is the code.
namespace Calculator{
public partial class Form1 : Form
{
string input = string.Empty;
string operand1 = string.Empty;
string operand2 = string.Empty;
char operation;
double result = 0.0;
public Form1()
{
InitializeComponent();
}
private void Zero_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
this.textBox1.Text = "";
input += "0";
this.textBox1.Text += input;
}
else if (textBox1.Text == "0")
{
}
}
private void One_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "1";
this.textBox1.Text += input;
}
private void Two_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "2";
this.textBox1.Text += input;
}
private void Three_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "3";
this.textBox1.Text += input;
}
private void Four_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "4";
this.textBox1.Text += input;
}
private void Five_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "5";
this.textBox1.Text += input;
}
private void Six_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "6";
this.textBox1.Text += input;
}
private void Seven_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "7";
this.textBox1.Text += input;
}
private void Eight_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "8";
this.textBox1.Text += input;
}
private void Nine_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "9";
this.textBox1.Text += input;
}
private void Decimal_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += ".";
this.textBox1.Text += input;
}
private void Divide_Click(object sender, EventArgs e)
{
operand1 = input;
operation = '/';
input = string.Empty;
}
private void Multiply_Click(object sender, EventArgs e)
{
operand1 = input;
operation = '*';
input = string.Empty;
}
private void Plus_Click(object sender, EventArgs e)
{
operand1 = input;
operation = '+';
input = string.Empty;
}
private void Minus_Click(object sender, EventArgs e)
{
operand1 = input;
operation = '-';
input = string.Empty;
}
private void Clear_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
}
private void Equal_Click(object sender, EventArgs e)
{
operand2 = input;
double.TryParse(operand1, out double num1);
double.TryParse(operand2, out double num2);
this.textBox1.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
if (operation == '/')
{
result = num1 / num2;
textBox1.Text = result.ToString();
}
else if (operation == '*')
{
result = num1 * num2;
textBox1.Text = result.ToString();
}
else if (operation == '+')
{
result = num1 + num2;
textBox1.Text = result.ToString();
}
else if (operation == '-')
{
if (num2 != 0)
{
result = num1 / num2;
textBox1.Text = result.ToString();
}
else
{
textBox1.Text = "ERROR: cannot divide by 0";
}
}
}
}
}
Conner Ferguson
解决方案
Maybe you should execute ‘input = result.ToString()’ after obtaining the result in Equal_Click.
这篇关于计算器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文