为什么我的最大数和最小数会显示不对
从文本框输入任意几个数字,然后计算出最高分 最低分 去掉最高分和最低分求出平均分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)
{
if (txtScore.Text == "")
{
MessageBox.Show("请输入分数!");
}
else
{
lvScore.Items.Add(txtScore.Text.Trim());
txtScore.Text = "";
count++;
}
}
private void btnClear_Click(object sender, EventArgs e)
{
lvScore.Items.Clear();
}
private void btnCaculate_Click(object sender, EventArgs e)
{
double sum=0,max,min,average;
for (int i = 0; i < count; i++)
{
sum += Convert.ToDouble(lvScore.Items);
max = Math.Max(Convert.ToDouble(lvScore.Items), Convert.ToDouble(lvScore.Items));
min = Math.Min(Convert.ToDouble(lvScore.Items), Convert.ToDouble(lvScore.Items));
average = (sum - max - min) / (count - 2);
lblResult.Text = "最大数:" + max.ToString() + "\n" + "最小数:" + min.ToString() + "\n" + "平均数:" + average.ToString();
}
}
private void txtScore_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox currentTextBox = sender as TextBox;
if (e.KeyChar < '0' || e.KeyChar > '9') //不是数字
{
e.Handled = true;
}
if (e.KeyChar == 8)
{
e.Handled = false;
}
if (e.KeyChar == 46)
{
if (currentTextBox.Text.IndexOf(".") == -1)
{
if (currentTextBox.SelectionStart > 0)
{
e.Handled = false;
}
}
}
}
}
}
这是我的代码
求大神一定要帮忙看看啊
private void btnCaculate_Click(object sender, EventArgs e)
{
double sum = 0;
double average = 0;
double max = Convert.ToDouble(lvScore.Items);
double min = Convert.ToDouble(lvScore.Items);
for (int i = 0; i < count; i++)
{
sum += Convert.ToDouble(lvScore.Items);
if (Convert.ToDouble(lvScore.Items) > max)
max = Convert.ToDouble(lvScore.Items);
if (Convert.ToDouble(lvScore.Items) < min)
min = Convert.ToDouble(lvScore.Items);
}
average = (sum - max - min) / (count - 2);
lblResult.Text =
"最大数:" + max.ToString() + "\n" +
"最小数:" + min.ToString() + "\n" +
"平均数:" + average.ToString();
} 因为你每次都和Items比较。
页:
[1]