.net 新手上路,已瘋->_<-

今天.net 上機實驗,已哭死在電腦前數組

把一些能簡單的全都簡單處理了。spa

 

 

 

1. 編寫一個控制檯應用程序,輸入三角形或者長方形邊長,計算其周長和麪積並輸出。.net

代碼以下:排序

using System;string

using System.Collections.Generic;it

using System.Linq;io

using System.Text;class

// a,b記錄長方形的長和寬變量

//a,b,d記錄三角形的三個邊循環

//c記錄周長,s記錄面積,e做爲判斷條件當e等於三時退出程序

//q用於變量轉換

//t 用於判斷三角形可否構成的判斷變量

namespace work1_1

{

    class Program

    {

        static void Main(string[] args)

        {

            int   e = 0;

            while(e != 3)

            {

            int a=0, b=0, c=0,s=0,d=0,q=0,t=1;

            Console.WriteLine("             三角形和長方形面積&周長");

            Console.WriteLine("             請選擇");

            Console.WriteLine("             1.長方形");

            Console.WriteLine("             2.三角形");

            Console.WriteLine("             3.退出");

            string p = Console.ReadLine();

            e = Convert.ToInt32(p);

            //Console.WriteLine(e);

            if (e == 3)

                break;

            else if (e == 1)

                {

                    Console.Write("請輸入矩形的長:");

                    p = Console.ReadLine();

                    a = Convert.ToInt32(p);

                  

                    Console.Write("請輸入矩形的寬:");

                    p = Console.ReadLine();

                    b = Convert.ToInt32(p);

                   

                    c = 2 * (a + b);

                    s = a * b;

                    Console.WriteLine("矩形的周長爲:" + c);

                    Console.WriteLine("矩形的面積爲:" + s);

                }

             else if (e == 2)

             {

                while(t != 0)

                 {

                 Console.Write("請輸入三角形的第一條邊:");

                 p = Console.ReadLine();

                 a = Convert.ToInt32(p);

 

                 Console.Write("請輸入三角形的第二條邊:");

                 p = Console.ReadLine();

                 b = Convert.ToInt32(p);

 

                 Console.Write("請輸入三角形的第三條邊:");

                 p = Console.ReadLine();

                 d = Convert.ToInt32(p);

 

                 if (a < b)

                 {

                     q = a;

                     a = b;

                     b = q;

                 }

                 if (a < c)

                 {

                     q = a;

                     a = d;

                     d = q;

                 }

                 if (a >= b + d)

                     Console.WriteLine("輸入的三角形的邊不能構成三角形請從新輸入!");

                 if (a < b + d)

                 {

                     c = a + b + d;

                     //秦九照公式 s=根號{c/2*(c/2-a)*(c/2-b)*(c/2-d)}

                     s =c/2*(c/2-a)*(c/2-b)*(c/2-d);

                     Console.WriteLine("三角形的周長爲:" + c);

                     Console.WriteLine("三角形的面積爲:" + Math.Sqrt(s));

                     t = 0;

                 }

 

                 }

                }

            }

        }

    }

}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

//3-5春季,6-8夏季,9-11秋季12-2冬季

namespace work1_2

{

    class Program

    {

 static void Main(string[] args)

        {

            int b = 1;

            while (b != 0)

            {

                Console.WriteLine("請輸入月份(輸入數字1-12):");

                int a;

                string s = Console.ReadLine();

                a = Convert.ToInt32(s);

                if (a == 3 || a == 4 || a == 5)

                {

                    Console.WriteLine("春季")

  b = 0;

                }

                else if (a == 6 || a == 7 || a == 8)

                {

                    b = 0;

                    Console.WriteLine("夏季");

                }

                else if (a == 9 || a == 10 || a == 11)

                {

                    b = 0;

 Console.WriteLine("秋季");

                }

                else if (a == 1 || a == 2 || a == 12)

                {

                    b = 0;

                    Console.WriteLine("冬季");

                }

                else

                    Console.WriteLine("輸入錯誤,從新輸入!");

            }

 }

    }

}

 

3. 編寫程序,用 while 循環語句實現下列功能:有一籃雞蛋,不止一個,有人兩個兩

個數,多餘一個,三個三個數,多餘一個,再四個四個地數,也多餘一個,請問這籃雞蛋至

少有多少個。

代碼以下:using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace work1_3

{

    class Program

    {

        static void Main(string[] args)

        {

            int s=1;

            while(s!=0){

  if (s % 2 == 1 && s % 3 == 1 && s % 4 == 1 && s!=1)

                    break;

                else

                    s++;

            }

            Console.WriteLine(s);

        }

    }

}

4. 編寫程序,計算數組中奇數之和和偶數之和。

代碼以下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace work1_4

{

    class Program

    {

        static void Main(string[] args)

        {

            //eNum記錄偶數之和

            //ueNum記錄奇數之和

            int eNum = 0, ueNum=0;

            int[] a = new int[10];

  for (int i = 1; i < 11; i++)

                a[i-1] = i;

            for (int i = 0; i < 10; i++)

            {

                if (a[i] % 2 == 1)

                    ueNum += a[i];

                else

                    eNum += a[i];

 }

  Console.WriteLine("數組中奇數和爲:"+ueNum);

            Console.WriteLine("數組中偶數和爲:" + eNum);

        }

    }

}

 

實驗結果如圖:

5. 編寫程序,找一找一個二維數組中的鞍點(即該位置上的元素值在行中最大,在該

列上最小。有可能數組沒有鞍點)。要求:

 二維數組的大小、數組元素的值在運行時輸入;

 程序有友好的提示信息。

代碼以下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace work1_5

{

    class Program

    {

        static void Main(string[] args)

        {

            //a,b記錄二維數組的長度,c記錄鞍點的最多數量

            //Num二維數組名

            //Max數組記錄最大值得下標,Min數組記錄最小值的下標

            //t 排序時的中介值

            int a, b, c;

 

            Console.WriteLine("請輸入二維數組的第一個維度長度:");

            a = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("請輸入二維數組的第二個維度長度:");

            b = Convert.ToInt32(Console.ReadLine());

            if (a < b)

                c = a;

            else

                c = b;

 

            double[,] Num = new double[a, b];

            double[] AnNum = new double[c];

            int[] Max = new int[2];

            int[] Min = new int[2];

            int i, j, k;

            Boolean flag = false;

            double t;

           

            //賦值

            for (i = 0; i < a; i++)

                for (j = 0; j < b; j++)

                {

                    Console.WriteLine("請給數組中的Num["+i+","+j+"]賦值");

                    Num[i,j] = Convert.ToInt32(Console.ReadLine()); ;

                }

 

 

            Console.WriteLine("該數組中的鞍點爲:");

 

            for (i = 0; i < a; i++)

            {

                Max [0] = i;

                Max [1] =  0;

                t = Num[i,0];

                for (j = 0; j < b - 1; j++)

                {

                    if (t < Num[i, j + 1])

                        Max[1] = j + 1;

                }

                Min[0] = 0;

                Min[1] = Max[1];

                t = Num[0,Max[1]];

                for (k = 0; k < a; k++)

                {

                    Min[1] = Max[1];

                    if (t>Num[k,Max[1]])

                            Max[0] = k;

                }

                if (Max[0] == Min[0] && Max[1] == Min[1])

                {

                    flag = true;

                    Console.WriteLine("第" + Max[0] + "行,第" + Max[1] + "列:" + Num[Max[0], Max[1]]);

                }

               

            }

            if(!flag)

                Console.WriteLine("無");

        }

    }

}

相關文章
相關標籤/搜索