为什么按了计算这个按钮运行起来会错误呢
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 Mark
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int count = 0;
private void button1_Click(object sender, EventArgs e)
{
lbScore.Items.Add(txtScore.Text.Trim());
txtScore.Text = "";
count++;
}
private void btnClear_Click(object sender, EventArgs e)
{
lbScore.Items.Clear();
}
private void btnCaculate_Click(object sender, EventArgs e)
{
double sum = 0;
double max = 0;
double min = 0;
double average = 0;
foreach (double c in lbScore.Items)
{
sum += c;
max = Math.Max(max, c);
min = Math.Max(min, c);
average = (sum - max - min) /(count-2);
}
lblResult.Text = "最大数:" + max.ToString()+"\n" + "最小数:" + min.ToString() +"\n"+ "平均数:" + average.ToString();
}
}
}
这是界面 这是输入几个数过后点击运算后显示的问题 下载看了把单击事件内容 换成这个
double sum = 0;
double max = 0;
double min = 0;
double average = 0;
foreach (var item in lbScore.Items)
{
Double c = Convert.ToInt32(item.ToString());
sum += c;
max = Math.Max(max, c);
min = Math.Max(min, c);
average = (sum - max - min) /(count-2);
}
lblResult.Text = "最大数:" + max.ToString()+"\n" + "最小数:" + min.ToString() +"\n"+ "平均数:" + average.ToString(); 把那个改成 toDouble
定义的是double 后面max.tostring()?
页:
[1]