【发布时间】:2022-01-23 23:11:21
【问题描述】:
我是 C# 新手,我有一些 Java 基础知识,但我无法让这段代码正常运行。
这只是一个基本的计算器,但是当我运行程序时 VS2008 给了我这个错误:
我做了几乎相同的程序,但在 java 中使用 JSwing 并且运行良好。
这是c#的形式:
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 calculadorac
{
public partial class Form1 : Form
{
int a, b, c;
String resultado;
public Form1()
{
InitializeComponent();
a = Int32.Parse(textBox1.Text);
b = Int32.Parse(textBox2.Text);
}
private void button1_Click(object sender, EventArgs e)
{
add();
result();
}
private void button2_Click(object sender, EventArgs e)
{
substract();
result();
}
private void button3_Click(object sender, EventArgs e)
{
clear();
}
private void add()
{
c = a + b;
resultado = Convert.ToString(c);
}
private void substract()
{
c = a - b;
resultado = Convert.ToString(c);
}
private void result()
{
label1.Text = resultado;
}
private void clear()
{
label1.Text = "";
textBox1.Text = "";
textBox2.Text = "";
}
}
可能是什么问题?有办法解决吗?
PS:我也试过
a = Convert.ToInt32(textBox1.text);
b = Convert.ToInt32(textBox2.text);
它没有用。
【问题讨论】:
标签: c# exception formatexception