對C#中幾個循環語句的使用,請教


            今天是在雲和數據學院學習的第四天,由於各種原因···今天自己預習的循環語句的用法以及寫了幾個程序,也遇到各種的問題了···糾結。由於還是在學習的很初初初級,所以好多簡單的方法還是不知道怎么寫出來,只得硬拉幾個循環語句。下面就看下我今天寫的十幾個小程序。

<1>.輸入班級人數,然后依次輸入學員成績,計算班級學員的平均成績和總成績

static void Main(string[] args)
        {
                int i = 1; int sum = 0; int avg = 0;
                Console.WriteLine("請輸入班級總人數:");
                int man = Convert.ToInt32(Console.ReadLine());
                for (; i <= man;i++)
                {
                    Console.WriteLine("請各輸入學生的成績:");
                    int score = Convert.ToInt32(Console.ReadLine());
                    sum = sum + score;
                    avg = sum / man;
                }
                Console.WriteLine("平均成績為{0},總成績為{1}", avg,sum);
                Console.ReadLine();
        }

<2>打印100次“歡迎您來雲和學院學習"

 static void Main(string[] args)
        {
            int i=1;
            while (i < 100)
            {
                Console.WriteLine("歡迎您來雲和學院學習");
            }
            Console.ReadLine();
        }

<3>2006年培養學員80000人,每年增長25%,請問按此增長速度,到哪一年培訓學員人數將達到20萬人?

 static void Main(string[] args)
        {
            double sdt = 80000;
            int year = 2006;
            sdt = 1.25 * sdt;
            do
            {
                 year++;
            }
            while(sdt > 200000);
            {
                 Console.WriteLine("到{0}培訓學員人數將達到20萬人",year);
                 Console.ReadKey();
              }
        }

<4>/老師問學生,這道題你會做了嗎?如果學生答"會了(y)",則可以放學.如果學生不會做(n),則老師再講一遍,再問學生是否會做了......
        -直到學生會為止,才可以放學.
        -直到學生會或老師給他講了10遍還不會,都要放學

static void Main(string[] args)
        {
            Console.WriteLine("這道題你會做了嗎?");
            Console.WriteLine("y/n");
            string anw = Console.ReadLine();
            if (anw =="y")
            {
                Console.WriteLine("可以放學");
            }
            else
            {
                Console.WriteLine("老實繼續講,這道題你會做了嗎?");
                Console.WriteLine("y/n");
                string anw1 = Console.ReadLine();
                if (anw1 == "y")
                {
                    Console.WriteLine("放學");
                }
                else
                {
                   int  i;
                   for (i=1; i <= 10; i++)
                   {
                       Console.WriteLine("老師再講一遍");
                       Console.WriteLine("y/n");
                   }
                   Console.WriteLine("都要放學");
                }
            }
            Console.ReadKey();
 }

     這個 整的我好迷茫啊,甚是不知道該怎么解決???真的是太無奈啊,學的只是還不多···想不出來該如何解決····請教。

<5>請用戶輸年份,再輸入月份,輸出該月的天數.(結合之前如何判斷閏年來做)

static void Main(string[] args)
        {
            Console.WriteLine("請用戶輸年份:");
            int year = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("請用戶輸月份:");
            int month = Convert.ToInt32(Console.ReadLine());
            switch (month)
            {
                case 1: 
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:Console.WriteLine("您輸入的月份共有31天");
                      break;
                case 2: 
                      bool a =year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
                      switch (a)
                      {
                        case true :   Console.WriteLine("您輸入的月份共有29天");
                            break;
                        case false:   Console.WriteLine("您輸入的月份共有28天");
                            break;
                       }
                      break;             
                case 4:
                case 6:
                case 9:
                case 11:Console.WriteLine("您輸入的月份共有31天");
                    break;
            }
            Console.ReadLine();
        }

     這個寫的還算滿意吧,但是我還是想有更好的方法吧,是不是下面分四種情況就輸出來啦那???

<6>  這個是嘗試下用while的用法:打印100次“歡迎您來雲和學院學習"

static void Main(string[] args)
        {
            int i=1;
            while (i < 100)
            {
                Console.WriteLine("歡迎您來雲和學院學習");
            }
            Console.ReadLine();
        }

<7>這個是練習下switch···case···的用法:對學員的結業考試成績評測(改成用Switch來做) 成績>=90:A   90>成績>=80:B     80>成績>=70:C    70>成績>=60:D    成績<60:E

static void Main(string[] args)
        {
            Console.WriteLine("請輸入你的成績");
            string Score = Console.ReadLine();
            int score = Convert.ToInt32(Score);
            switch (score/10)
            {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6: Console.WriteLine("E");
                    break;
                case 7: Console.WriteLine("E");
                    break;
                case 8: Console.WriteLine("E");
                    break;
                case 9: Console.WriteLine("E");
                    break;
            }
            Console.ReadLine();
        }

   感覺switch用着好痛苦啊!!!  還是if···else···好用,簡單易懂,哇咔咔.

<8>這個也是while的用法,感覺好亂啊,嘿嘿   程序要求:要求用戶輸入用戶名和密碼,只要不是admin、888888就一直提示用戶名或密碼錯誤,請重新輸入。

 static void Main(string[] args)
        {
            while (true)
            {
            Console.WriteLine("請輸入用戶名:");
            string a = Console.ReadLine();
            Console.WriteLine("請輸入密碼:");
            string b = Console.ReadLine();
            if (a != "admin" || b != "888888")
            {
                Console.WriteLine("用戶名或密碼錯誤,請重新輸入.");
            }
            else
            {
                Console.WriteLine("用戶名或密碼正確,請進入.");
            }
            Console.ReadKey();
        }
    }

<9>for循環來啦,激動啊,嘿嘿 程序要求:計算1到100之間整數的和

static void Main(string[] args)
        {
            int i = 1;
            int sum = 0;
            for (; i <= 100; i++)
            {
                sum =sum+ i;
            }
            Console.WriteLine("輸出1到100之間整數的和"+sum);
            Console.ReadLine();
        }

<10>明天小蘭就要登台演出了,老師說再把明天的演出的歌曲唱一遍,
         如果滿意,小蘭就可以回家了.
         否則就需要再練習一遍,直到老師滿意為止.(y/n)

static void Main(string[] args)
        {
            Console.WriteLine("小蘭唱歌滿意不滿意,如果滿意可以回家,不滿意繼續練習");
            string a = Console.ReadLine();
            int i = 0;
            do
            {

                if (a == "n")
                {
                    Console.WriteLine("繼續練習");
                    a = Console.ReadLine();
                    i++;
                }
                else
                {
                    Console.WriteLine("可以回家");

                }
            
            }
            while (a == "n"&&i<=3);
            {
            }

            {
                Console.WriteLine("小蘭唱歌滿意");
                Console.WriteLine("滿意可以回家");
                Console.ReadKey();

            }
        }

     這個用的do···while···太糾結啦啦啦,不知道是在do里面寫循環體還是寫這樣的條件,我想說我就這樣運行除惡要的結果啦。

<11>不斷要求用戶輸入學生姓名,輸入q結束

static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("請輸入學生姓名:");
                string input = Console.ReadLine();
                if (input == "q")
                {
                    break;
                }
                else
                {
                    Console.WriteLine("請再次輸入學生姓名:");
                }
                Console.ReadKey();
            }
        }

   這個輸出的結果好逗,但是要求的還是循環出來啦,只是感覺程序這樣寫還是感覺不對啊

<12>下面這個也是用的switch···case···,感覺不怎么好玩,還是if···else···用起來簡單方便啊,程序要求:李四的年終工作評定,如果定為A級,則工資漲500元,如果定為B級,則工資漲200元,如果定為C級,工資不變,如果定為D級工資降200元,如果定為E級工資降500元.設李四的原工資為5000,請用戶輸入李四的評級,然后顯示李四來年的工資.

 static void Main(string[] args)
        {
            Console.WriteLine("請輸入對李四的評定等級(A-E)?");
            string str = Console.ReadLine();
            decimal salary = 5000;
            bool flag = false;
            switch (str)
            {
                case "A":
                    salary += 500;
                    break;
                case "B":
                    salary += 200;
                    break;
                default:
                    Console.WriteLine("你的輸入有問題!");
                    flag = true;
                    break;
                case "C":
                    break;
                case "D":
                    salary -= 200;
                    break;
                case "E":
                    salary -= 500;
                    break;
            }
            if (flag == false)
            {
                Console.WriteLine("李四的工資為:" + salary);
            }
            Console.ReadKey();
        }

<f13>for循環來啦:求1-100間的所有偶數和

static void Main(string[] args)
        {
            int i = 0; int sum = 0;
            for (; i <= 100; i=i+2)
            {
                sum = sum + i;
            }
            Console.WriteLine("1-100間的所有偶數和為" + sum);
            Console.ReadKey();
        }

      好啦,先寫這幾個吧,我學習.net的歷程····

      明天可以學習下這2個題目啦吧

•找出100-999間的水仙花數?

•問題3:輸出九九乘法表(循環的嵌套,嘿嘿 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM