C#怎么用代码编写一个三个数比大小的窗体应用程序
private void BtnCompare_Click(object sender, EventArgs e)
{
int max;
int min;
if(GetMaxAndMin(textBox1.Lines,out max,out min))
{
textBox2.Text = max.ToString();
textBox3.Text = min.ToString();
}
}
private bool GetMaxAndMin(string[] strArr,out int max,out int min)
{
bool b = true;
int maxValue=int.MinValue;
int minValue=int.MaxValue;
for(int i=0;i<strArr.Length;i++)
{
int num;
if(int.TryParse(strArr[i],out num))
{
if(num<minValue)
{
minValue = num;
}
if(num>maxValue)
{
maxValue = num;
}
}
else
{
MessageBox.Show("含有非数字字符,请重新输入!");
b = false;
max = maxValue;
min = minValue;
break;
}
}
max = maxValue;
min = minValue;
return b;
}
急求啊
我早晨就把代码给你了.
double tmp;
string[] num = txtno.Text.Split(new char[] { ',','\r','\n' });
double[] no = new double[num.Length];
for (int i = 0; i < num.Length; i++)
{
no[i] = Convert.ToDouble(num[i]);
}
for (int j = 0; j < num.Length - 1; j++)
{
for (int k = 0; k < num.Length - j - 1; k++)
{
if (no[k] > no[k + 1])
{
tmp = no[k];
no[k] = no[k + 1];
no[k + 1] = tmp;
}
}
}
MessageBox.Show("最小值:" + no[0] + "最大值:" + no[num.Length-1]);
int max=0,sm=0;
int s1=Convert.toInt32(textbox1.value);
int s2=Convert.toInt32(textbox2.value);
int s3=Convert.toInt32(textbox3.value);
if(max<s1)max=s1;
if(max<s2)max=s2;
if(max<s3)max=s3;
sm=max;
if(sm>s1)sm=s1;
if(sm>s2)sm=s2;
if(sm>s3)sm=s3;
textbox4.value=max.toString();//最大值
textbox5.value=sm.toString();//最小值
我记错了,应该写成:
int max=0,sm=0;
int s1=Convert.ToInt32(textbox1.text.trim());
int s2=Convert.ToInt32(textbox2.text.trim());
int s3=Convert.ToInt32(textbox3.text.trim());
if(maxs1)sm=s1;
if(sm>s2)sm=s2;
if(sm>s3)sm=s3;
textbox4.text=max.ToString();//最大值
textbox5.text=sm.ToString();//最小值