
C#做一个计算器出现了字符串输入的格式不正确,求解答~附代码~~~
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 计算器{ public partial class 计算器 : Form { string x; public 计算器() { InitializeComponent(); } private void button4_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { Double num1 = Convert.ToDouble(this.textBox1.ToString()); Double num2 = Convert.ToDouble(this.textBox2.ToString()); string ch = x; String Result = ""; switch (ch) { case "+": Result = (num1 + num2).ToString(); this.textBox3.Text = Result; break; case "-": Result = (num1 - num2).ToString(); this.textBox3.Text = Result; break; case "*": Result = (num1 * num2).ToString(); this.textBox3.Text = Result; break; case "/": Result = (num1 / num2).ToString(); this.textBox3.Text = Result; break; default: break; } } private void button2_Click(object sender, EventArgs e) { Application.Exit(); } private void 计算器_Load(object sender, EventArgs e) { } private void label3_Click(object sender, EventArgs e) { } private void radioButton3_CheckedChanged(object sender, EventArgs e) { x = "+"; } private void button3_Click(object sender, EventArgs e) { this.textBox1.Clear(); this.textBox2.Clear(); this.textBox3.Clear(); } private void textBox3_TextChanged(object sender, EventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { } private void radioButton2_CheckedChanged(object sender, EventArgs e) { x = "-"; } private void radioButton1_CheckedChanged(object sender, EventArgs e) { x = "*"; } private void radioButton4_CheckedChanged(object sender, EventArgs e) { x = "/"; } }}
展开
2个回答
2017-10-22
展开全部
转成int类型就好,double没必要,而且,报错的地方你写成textbox直接转换了,要.一下出来text属性,这个属性才是你输入的字符的值,而这个属性本来就是string的,无需tostring()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询