天天看點

c#while循環

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _0909while循環
{
    class Program
    {
        static void Main(string[] args)
        {
            //int day = 0;
            //Console.WriteLine("請輸入一個年份:");
            //try
            //{
            //    int year = Convert.ToInt32(Console.ReadLine());
            //    Console.WriteLine("請輸入一個月份:");
            //    try
            //    {
            //        int month = Convert.ToInt32(Console.ReadLine());
            //        if (month >= 1 && month <= 12)
            //        {
            //            switch (month)
            //            {
            //                case 1:
            //                case 3:
            //                case 5:
            //                case 7:
            //                case 8:
            //                case 10:
            //                case 12:
            //                    day = 31;
            //                    break;
            //                case 2:
            //                    if ((year % 400 == 0) || (year % 4 == 0 && year % 100 == 0))
            //                    {
            //                        day = 29;
            //                    }
            //                    else
            //                    {
            //                        day = 28;
            //                    }
            //                    break;
            //                case 4:
            //                case 6:
            //                case 9:
            //                case 11:
            //                    day = 30;
            //                    break;
            //            }
            //            Console.WriteLine("{0}年{1}月{2}天",year,month,day);
            //        }
            //        else
            //        {
            //            Console.WriteLine("輸入的月份有太大了");
            //        }
            //    }
            //    catch
            //    {
            //        Console.WriteLine("輸入的月份有錯誤");
            //    }
            //}
            //catch
            //{
            //    Console.WriteLine("輸入的年份有錯誤");
            //}
            //Console.ReadKey();

            //向控制台列印100遍“”
            //循環體  需要執行的語句
            //循環條件 列印的次數小于100遍
            //int i = 0;
            //while (i < 100)
            //{
            //    Console.WriteLine("努力賺錢");
            //    i++;//每循環一次,i的值都要自增加1,否則将陷入死循環
            //}
            //Console.ReadKey();
            //1-100之間所有偶數的和
            //int i = 1;
            //int sum = 0;
            //while (i <= 100)
            //{
            //    if (i % 2 == 0)
            //    {
            //        sum += i;
            //    }
            //    i++;
            //}
            //Console.WriteLine(sum);
            //Console.ReadKey();
            //break 跳出目前循環

            //int i = 1;          
            //while (i < 10)
            //{ 
            //    int j = 1;
            //    while (j <= i)
            //    {
            //        Console.WriteLine("{0}*{1}={2} ", i, j, i * j);
            //        j++;
            //    }
            //    i++;                
            //}
            //Console.ReadKey();

            //循環體
            //循環條件  使用者名或者密碼錯誤
            //string useName = "";
            //string passWord = "";
            //while (useName != "admin" || passWord != "888888")
            //{
            //    Console.WriteLine("請輸入使用者名");
            //     useName = Console.ReadLine();
            //    Console.WriteLine("請輸入密碼");
            //    passWord = Console.ReadLine();
            //}
            //Console.WriteLine("登陸成功");
            //Console.ReadKey();

            //輸入班級人數,依次計算班級人數的平均成績和總成績
            //循環體  提示輸入學員成績,接收并轉換成整數類型,并依次累加到總成績當中
            //循環條件  小于等于班級人數
            //int i = 1;//記錄循環次數
            //int sum = 0;
            //Console.WriteLine("請輸入班級人數");
            //int count = Convert.ToInt32(Console.ReadLine());
            //while (i <= count)
            //{
            //    Console.WriteLine("請輸入第{0}個學員的成績",i);
            //    int score = Convert.ToInt32(Console.ReadLine());
            //    sum += score;
            //    i++;
            //}
            //Console.WriteLine("人數為{0}人的班級總成績是{1},平均成績是{2}",count,sum,sum/count);
            //Console.ReadKey();
            //string answer = "";
            //int i = 1;

            //while(answer != "yes" && i<=10)
            //{
            //    Console.WriteLine("第{0}遍,你會了嗎?yes/no",i);
            //    answer = Console.ReadLine();
            //    if (answer == "yes")
            //    {
            //        Console.WriteLine("放學");
            //        break;
            //    }               
            //        i++;                
            //}
            //Console.ReadKey();

            //string input = "";
            //while (input != "yes" && input != "no")
            //{
            //    Console.WriteLine("請輸入yes或者no");
            //    input = Console.ReadLine();

            //}
            //Console.ReadKey();

            //string name = "";
            //string pwd = "";
            //do
            //{
            //    Console.WriteLine("請輸入使用者名");
            //    name = Console.ReadLine();
            //    Console.WriteLine("請輸入密碼");
            //    pwd = Console.ReadLine();;

            //} while (name != "admin" || pwd != "888888");
            //Console.WriteLine("登入成功");
            //Console.ReadKey();

            //不斷要求使用者輸入學生姓名,輸入q結束
            //string name = "";

            //do
            //{
            //    Console.WriteLine("請輸入學員姓名");
            //    name = Console.ReadLine();
            //} while (name!="q");
            //while (name != "q")
            //{
            //    Console.WriteLine("請輸入學員姓名");
            //    name = Console.ReadLine();
            //}
            //Console.ReadKey();

            //string input = "";
            //while (input !="q")
            //{
            //    //輸入不能直接轉換成int類型,因為有可能輸入的是q
            //    Console.WriteLine("請輸入一個數字");
            //    input = Console.ReadLine();
            //    if (input != "q")
            //    {
            //        try
            //        {

            //            int number = Convert.ToInt32(input);
            //            Console.WriteLine(number * 2);
            //        }
            //        catch
            //        {
            //            Console.WriteLine("輸入的字元串不能轉換成數字");
            //        }
            //    }
            //    else
            //    {
            //        Console.WriteLine("輸入的是q,程式退出");
            //    }
            //}
            //Console.ReadKey();

        }
    }
}      

繼續閱讀