namespace WinFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e) //事件textbox2按了enter键
{
if (e.KeyChar == 13) //为enter键
{
int a, b;
bool toushu = int.TryParse(textBox1.Text, out a);
bool jiaoshu = int.TryParse(textBox2.Text, out b); //头、脚数是否可以转为整型
if (toushu == true & jiaoshu == true)
{
float c, d;
c = 2 * a - b / 2; //方程的鸡解
d = b / 2 - a; //方程的兔解
string str1 = c.ToString();
string str2 = d.ToString(); //转换为字符串
int C, D;
bool jishu = int.TryParse(str1, out C); //解是否可以转换为整型
bool tushu = int.TryParse(str2, out D);
if (jishu == true & tushu == true & C >= 0 & D >= 0) //合理解
label4.Text = " "+str1 + " " +str2; //输出
else
MessageBox.Show("error");
}
else
MessageBox.Show("error");
}
}
}
}
鸡兔同笼问题用C#解决
输入脚数之后按回车键即可算出答案