我试图为一个带有三个整数输入的分配创建一个计算器并取平均值并将其返回给用户。我使用Visual Basic(它是必需的)来制作GUI。我遇到两件事情有困难,首先,我不能让aVer除以3,因为它不是整数,其次,我不知道如何在上一个文本框中获得平均值的输出。
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string userInfo1;
private string userInfo2;
private string userInfo3;
private string aVer;
private string num1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int aVer = 0;
aVer = Int32.Parse(userInfo1 + userInfo2 + userInfo3);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int userInfo1 = 0;
userInfo1 = Int32.Parse(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
int userInfo2 = 0;
userInfo2 = Int32.Parse(textBox2.Text);
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
int userInfo3 = 0;
userInfo3 = Int32.Parse(textBox3.Text);
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
int num = Int32.Parse(aVer);
MessageBox.Show(num.ToString());
}
private void label3_Click(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:0)
您多次声明变量。函数调用中的变量将隐藏您在类声明外部定义的变量,并且类变量永远不会被设置为值。
您似乎没有使用其字符串形式的任何变量,因此所有private string
声明都应更改为private int
声明。这也使int userInfo1 = 0;
声明变得不必要。让它们实际上破坏了查看button1_Click
函数中的值的能力。