實現成績表的初步想法

來源:互聯網
上載者:User

標籤:blog   http   for   ar   資料   2014   div   log   sp   

 利用結構體可以實現我們手動輸入一些學生資訊,進行自動排序列印的小程式;

首先在Main函數外面定義一個名字為student的結構體,下面沒一個同學的資訊儲存都需要用到student結構體。

struct student
        {
            public int no;
            public string name;
            public int Cshap;
            public int web;
            public int datebase;
            public int sum;
      
        }  //定義一個student結構體

下面為Main函數開始;

 static void Main(string[] args)

{
            int i;  int j;//聲明迴圈變數
            int no_x;string name_x;int Cshap_x;int web_x;int datebase_x;//定義元素變數

上面主要是定義兩個迴圈變數i和j,i用來進行輸入迴圈,j用來進行排序迴圈。no_x是輸入學號的一個變數,name_x是輸入名字的一個變數,Cshap_x是輸入Cshap成績的一個變數,web_x是輸入網頁成績的一個變數,datebase_x是輸入資料庫成績的一個變數。

            student a = new student();
            student b = new student();
            student c = new student();
            student d = new student();       //定義用於分別存放三個學產生績的結構體,d用來進行交換

上面定義了4個student類型的結構體,a、b、c是存放三個學產生績的結構體,d是在排序的時候用來交換資料的一個結構體。

 

下面是輸入迴圈開始,總共需要輸入三個學生的資訊,因此i<=3,學號是自動+1,其他的資訊都是需要主動輸入,輸入一個同學完成之後清一下屏。

 for (i = 1; i <=3; i++)
            {
                //提示輸入資料,並儲存在元素變數裡
                no_x = i;
                Console.WriteLine("請輸入第{0}位同學的姓名:", i);
                name_x = Console.ReadLine();
                Console.WriteLine("請輸入第{0}位同學的Cshap成績:", i);
                Cshap_x = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("請輸入第{0}位同學的網頁成績:", i);
                web_x = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("請輸入第{0}位同學的資料庫成績:", i);
                datebase_x = Convert.ToInt32(Console.ReadLine());

                Console.Clear();

//下面判斷是不是第一個同學,是的話將他的基本資料存入第一個同學的結構體中,也就是a中。

 if(i==1)
                {
                        a.no = no_x;
                        a.name = name_x;
                        a.Cshap = Cshap_x;
                        a.web =web_x;
                        a.datebase = datebase_x;
                        a.sum = a.Cshap + a.web + a.datebase;

                }

//下面判斷是不是第二個同學,是的話將他的基本資料存入第二個同學的結構體中,也就是b中。

                if (i == 2)
                {
                  
                        b.no = no_x;
                        b.name = name_x;
                        b.Cshap = Cshap_x;
                        b.web = web_x;
                        b.datebase = datebase_x;
                        b.sum = b.Cshap + b.web + b.datebase;
                  
                }

//下面判斷是不是第三個同學,是的話將他的基本資料存入第三個同學的結構體中,也就是c中。

                if (i == 3)
                {
                   
                        c.no = no_x;
                        c.name = name_x;
                        c.Cshap = Cshap_x;
                        c.web = web_x;
                        c.datebase = datebase_x;
                        c.sum = c.Cshap + c.web + c.datebase;
                  
                }

 }

//儲存完資料之後,開始排序,排序因為這裡是用的a,b,c來儲存的資料,暫時沒想到直接用兩套迴圈來冒泡排序的方法,可以用一個數組zf[]來存一下每個同學的總分,這樣就可以用兩套迴圈來進行排序

                for (j = 0; j < 2; j++)
                {
                    if (a.sum < b.sum)
                    {
                        d = a;
                        a = b;
                        b = d;
                    }
                     if (b.sum < c.sum)
                    {
                        d = b;
                        b = c;
                        c = d;
                       
                    }
                }

 //排序完成,進行列印,因為前面的語句已經將順序排好,總分高的同學的資料放在了a裡面,次高的放在b裡面,低的放在c裡面,所以列印直接按a,b,c列印就可以

            Console.WriteLine("學號\t姓名\tCshap\t網頁\t資料庫\t總分\t");
            Console.WriteLine(("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t"),a.no,a.name,a.Cshap,a.web,a.datebase,a.sum);
            Console.WriteLine(("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t"), b.no,b.name,b.Cshap, b.web, b.datebase, b.sum);
            Console.WriteLine(("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t"), c.no, c.name, c.Cshap, c.web, c.datebase, c.sum);

  }
        }
    }

基本實現成績表的一個想法,但是當學生數量多的時候程式就顯得很複雜,還需要用別的方法再試,最好能用迴圈解決

下面看一下效果:

輸入學生資訊的介面: 輸入完成之後列印出的介面:   程式網盤地址:http://pan.baidu.com/s/1qWNmAUo

實現成績表的初步想法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.