输入三个No,平均只需输入两个更大的No. [英] Enter the three nos, need average of only two greater nos.

查看:86
本文介绍了输入三个No,平均只需输入两个更大的No.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要平均两个大个数,请帮助我.

Hi, i need avg of two greater nos, so kindly help me.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data;

public partial class HomePage : System.Web.UI.Page
{
    adminDB admDB;
    protected void Page_Load(object sender, EventArgs e)
    {
        txtRegNo.Focus();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       

        //Retrive Marks
        try
        {
            DataSet ds = new DataSet();
            admDB = new adminDB();
            ds = admDB.RetriveIAMArks1(txtRegNo.Text, txtIA1.Text, ddlBranch.Text);
            if (ds.Tables[0].Rows.Count > 0)
            {
                lblmsg.Visible = false;
                gvNames.Visible = true;
                gvNames.DataSource = ds;
                gvNames.DataBind();
                lbl1.Text= ds.Tables[0].Rows[0][7].ToString();
            }
            else
            {
                gvNames.Visible = true;
                lblmsg.Text = "No results found!";
            }

            admDB = new adminDB();
            ds = admDB.RetriveIAMArks2(txtRegNo.Text, txtIA2.Text, ddlBranch.Text);
            if (ds.Tables[0].Rows.Count > 0)
            {
                lblmsg.Visible = false;
                gvNames.Visible = true;
                gvNames.DataSource = ds;
                gvNames.DataBind();
                lbl2.Text = ds.Tables[0].Rows[0][7].ToString();
            }
            else
            {
                gvNames.Visible = true;
                lblmsg.Text = "No results found!";
            }

            admDB = new adminDB();
            ds = admDB.RetriveIAMArks3(txtRegNo.Text, txtIA3.Text, ddlBranch.Text);
            if (ds.Tables[0].Rows.Count > 0)
            {
                lblmsg.Visible = false;
                gvNames.Visible = true;
                gvNames.DataSource = ds;
                gvNames.DataBind();
                lbl3.Text = ds.Tables[0].Rows[0][7].ToString();
            }
            else
            {
                gvNames.Visible = true;
                lblmsg.Text = "No results found!";
            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.ToString();
        }

        try
        {
            DataSet ds = new DataSet();
            admDB = new adminDB();
            ds = admDB.ViewAssignmentbyRegNo(txtRegNo.Text, ddlBranch.Text);
            if (ds.Tables[0].Rows.Count > 0)
            {
                lblmsg.Visible = false;
                gvNames.Visible = true;
                Label7.Visible = true;
                gvNames.DataSource = ds;
                gvNames.DataBind();

                int ia1, ia2, 1a3;
                decimal avg;
                ia1 = Convert.ToInt16(lbl1.Text);
                ia2 = Convert.ToInt16(lbl2.Text);
                
                avg = (ia1 + ia2 + ia3 ) / 2;
                lblavg.Text = avg.ToString();
            }
            else
            {
                lblmsg.Visible = true;
                gvNames.Visible = false;
                lblmsg.Text = "No results found!";
            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.ToString();
        }

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("HomePage.aspx");
    }
}

推荐答案

int[] intArray = new int[3] {ia1,ia2,ia3 }; //add all three value in intarray

Array.Sort(intArray); // sort them in ascending order 

avg = (intArray [1]+intArray [2]) / 2; // add largest and second largest no and divide it by 2

lblavg.Text = avg.ToString(); // show them in label



我还没有测试过..
但希望它能运行...

[edit]添加了代码块[/edit]



i haven''t tested it..
but hoping it will run...

[edit]code block added[/edit]



List<int> lstInt = new List<int>();
          lstInt.Add(10);
          lstInt.Add(6);
          lstInt.Add(14);

          lstInt.Sort();
          lstInt.Reverse();
          double x = lstInt.Take(2).Average();


这篇关于输入三个No,平均只需输入两个更大的No.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆