Home > Net >  Simple c # calculator
Simple c # calculator

Time:09-20

Beginners, have been doing a simple calculator, now make it can run without error, but each time to the computing result no results, and suggests there is a place there is a problem,
Saw a lot of online related problems or errors, and strives for the bosses to help see the zha to
 

Using System;
using System.Collections.Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
Using System. Linq;
Using System. The Text;
Using System. Windows. Forms;

The namespace WindowsFormsApplication2
{
Public partial class Compute: Form
{
Private string s.
Private double num1=0;
Private double num2=0;
The Button BTN.

Public Compute ()
{
InitializeComponent ();
}
Private void btnNumber_Click (object sender, EventArgs e)
{
BTN=(Button) sender;
TxtDisplay. Text=txtDisplay. Text + BTN. Text;
}

Private void btnOp_Click (object sender, EventArgs e)
{
BTN=(Button) sender;
If (BTN Name!="btnSign")
{
Num1=the Convert. ToDouble (txtDisplay. Text);
TxtDisplay. Text="";
S=BTN. Name;
}
The else
{
Num2=the Convert. ToDouble (txtDisplay. Text);
The switch (s)
{
Case "btnAnd" :
TxtDisplay. Text=(num1 + num2). The ToString ();
break;
Case "btnSubstract" :
TxtDisplay. Text=(num1 - num2). The ToString ();
break;
Case "btnMultiply" :
TxtDisplay. Text=(num1 * num2). The ToString ();
break;
Case "btnDivide" :
If (num2==0)
MessageBox. Show (" divisor cannot be zero!" );
The else
TxtDisplay. Text=(num1/num2). The ToString ();
break;
}
}
}
Private void btnC_Click (object sender, EventArgs e)
{
TxtDisplay. Text="";
}

Private void Form1_Load (object sender, EventArgs e)
{

}


}
}


CodePudding user response:

Error messages have tip: format is not correct, you cannot convert,
In most cases is null,
Add a empty judgment
The
reference
 if (txtDisplay. Text. The Trim ()!="") 
{
Try
{
Num1=the Convert. ToDouble (txtDisplay. Text);
} catch {
//input format is not correct
Num1=0;
}
The finally {
TxtDisplay. Text="";
S=BTN. Name;
}

}
The else
{
//null
S=BTN. Name;
Num1=0;
}

CodePudding user response:

Format conversion problem, it is suggested that txtDisplay. Text=""; Don't empty, is set to 0

CodePudding user response:

See yourself what is digital input, it is recommended to use Double. Alternative Convert TryParse. ToDouble

CodePudding user response:

reference 1st floor xugq979 response:
error has tip: format is not correct, you cannot convert,
In most cases is null,
Add a empty judgment
The
reference
 if (txtDisplay. Text. The Trim ()!="") 
{
Try
{
Num1=the Convert. ToDouble (txtDisplay. Text);
} catch {
//input format is not correct
Num1=0;
}
The finally {
TxtDisplay. Text="";
S=BTN. Name;
}

}
The else
{
//null
S=BTN. Name;
Num1=0;
}

Me with this, you can run no follow-up report errors, but still not running results,
My digital class button name is set is the number1, number2,
Rather than num1, num2
Because I write in the code before number1 these two lines always complains and then write the num1 will not
I don't know if it has something to do with this,,

CodePudding user response:

reference 5 floor Perfume_ reply:
Quote: refer to 1st floor xugq979 response:

Error messages have tip: format is not correct, you cannot convert,
In most cases is null,
Add a empty judgment
The
reference
 if (txtDisplay. Text. The Trim ()!="") 
{
Try
{
Num1=the Convert. ToDouble (txtDisplay. Text);
} catch {
//input format is not correct
Num1=0;
}
The finally {
TxtDisplay. Text="";
S=BTN. Name;
}

}
The else
{
//null
S=BTN. Name;
Num1=0;
}

Me with this, you can run no follow-up report errors, but still not running results,
My digital class button name is set is the number1, number2,
Rather than num1, num2
Because I write in the code before number1 these two lines always complains and then write the num1 will not
I don't know if it has something to do with this,,

This sentence or not as a result, you make me dizzy,,,
 if (BTN Name!="btnSign") 
{

TxtDisplay. Text="";

}

If the BTN name is not equal to btnSign, no is not the result,,,, the logic of the code you write above,,,

CodePudding user response:

Develop a good habit, add a comment,

CodePudding user response:

 
If (BTN Name!="btnSign")
{
Num1=the Convert. ToDouble (txtDisplay. Text);
TxtDisplay. Text="";
S=BTN. Name;
}
The else
{
Num2=the Convert. ToDouble (txtDisplay. Text);
The switch (s)
{
Case "btnAnd" :
TxtDisplay. Text=(num1 + num2). The ToString ();
break;
Case "btnSubstract" :
TxtDisplay. Text=(num1 - num2). The ToString ();
break;
Case "btnMultiply" :
TxtDisplay. Text=(num1 * num2). The ToString ();
break;
Case "btnDivide" :
If (num2==0)
MessageBox. Show (" divisor cannot be zero!" );
The else
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related