我的问题是我运行此代码显示错误(其计算器程序) [英] MY PROBLEM IS WHEN I RUN THIS CODE SHOW ERROR (its calculator program)
本文介绍了我的问题是我运行此代码显示错误(其计算器程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
命名空间 WindowsFormsApplication35
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click( object sender,EventArgs e)
{
if (textBox1.Text == 0)
{
textBox1.Clear();
}
按钮按钮=(按钮)发送者;
textBox1.Text = textBox1.Text + button.Text;
}
私有 void button16_Click( object sender,EventArgs e)
{
Button button =(Button)sender;
textBox1.Text = textBox1.Text + button.Text;
string 结果;
string st;
st = button.Text;
double num1 = Double .Parse(textBox1.Text); // 这里显示错误如何解决这个问题
textBox1.Text = 字符串跨度> .Empty;
double num2 = Double .Parse(textBox1.Text);
switch (st)
{
case +:
result =(num1 + num2).ToString();
textBox1.Text = Convert.ToString(result);
break ;
case - :
result =(num1 - num2).ToString();
textBox1.Text = Convert.ToString(result);
break ;
case * :
result =(num1 * num2).ToString();
textBox1.Text = Convert.ToString(result);
break ;
case / :
if (num2!= 0 )
{
result =(num1 / num2).ToString();
textBox1.Text = Convert.ToString(result);
}
else
{
MessageBox.Show( 不能除以零);
}
break ;
}
}
}
}
我尝试了什么:
输入数字并按添加按钮时显示错误信息。
请解决问题.......
解决方案
由于cvogt61457注明了{type如果字符串不是目标类型的有效表示,则.Parse方法将抛出异常。其中{type}通常是整数,double,boolean。请改用{type} .TryParse()方法。
double num1 = 0 。 0 ;
if ( Double .TryParse(textBox1.Text, out num1)){
// 使用num1
}
其他 {
// 告诉用户他或她是一个凝块。
}
我不明白为什么要转换结果
到字符串
textBox1.Text = Convert.ToString(result);
,因为结果
已经是一个字符串
result =(num1 + num2).ToString ();
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication35
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Clear();
}
Button button = (Button)sender;
textBox1.Text = textBox1.Text + button.Text;
}
private void button16_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
textBox1.Text = textBox1.Text + button.Text;
string result;
string st;
st = button.Text;
double num1 = Double.Parse(textBox1.Text);// here show the error how to slove this
textBox1.Text = String.Empty;
double num2 = Double.Parse(textBox1.Text);
switch (st)
{
case "+":
result = (num1+num2).ToString();
textBox1.Text = Convert.ToString(result);
break;
case "-":
result = (num1 - num2).ToString();
textBox1.Text = Convert.ToString(result);
break;
case "*":
result = (num1 * num2).ToString();
textBox1.Text = Convert.ToString(result);
break;
case "/" :
if (num2 != 0)
{
result = (num1 / num2).ToString();
textBox1.Text = Convert.ToString(result);
}
else
{
MessageBox.Show("Can't divide by zero");
}
break;
}
}
}
}
What I have tried:
when enter a number and press add button its show the error message .
please solve the problem .......
解决方案
As cvogt61457 noted a {type}.Parse method will throw an exception if the string isn't a valid representation of the target type. Where {type} is commonly integer, double, boolean. Use {type}.TryParse() methods instead.
double num1 = 0.0; if (Double.TryParse(textBox1.Text, out num1)) { // Do stuff with num1 } else { // Tell user he or she is a clot. }
I don't understand why you convertresult
to string
textBox1.Text = Convert.ToString(result);
sinceresult
is already a string
result = (num1+num2).ToString();
这篇关于我的问题是我运行此代码显示错误(其计算器程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文