條件陳述式練習

來源:互聯網
上載者:User

標籤:

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

namespace ConsoleApplication1
{
        class Program
        {
        static void Main(string[] args)
        {
        //相親,“你有房子嗎?”有,“咱們結婚吧”;沒有,“你有錢嗎?”有,“先買房,再結婚吧。”沒有,
        //“你有能力嗎?”有,“先賺錢,再買房,再結婚吧!”,沒有,“拜拜!”
        Console.WriteLine("你有房子嗎?");
        string huida = Console.ReadLine();//因為回答不是一個數字,需要string定義
        if (huida == "有")
        {
              Console.WriteLine("咱們結婚吧!");
        }
        else//沒有
        {
             Console.WriteLine("你有錢嗎?");
             huida = Console.ReadLine();
             if (huida == "有")
             {
                   Console.WriteLine("先買房再結婚吧!");
              }
             else//沒有
             {
                   Console.WriteLine("你有能力嗎?");
                   huida = Console.ReadLine();
                   if (huida == "有")
                   {
                        Console.WriteLine("先賺錢,再買房,再結婚吧!");
                   }
                   else //沒有
                   {
                        Console.WriteLine("拜拜!");
                   }
              }

        }

       Conso.readLine();

 

 

 

        

//分別輸入月份 幾號 輸出是今年的第多少天,每年的1 3 5 7 8 10 12是31天,今年的2月是28天,其他是30天
int m1 = 31, m2 = 28, m3 = 31, m4 = 30, m5 = 31, m6 = 30, m7 = 31, m8 = 31, m9 = 30, m10 = 31, m11 = 30;
//分別定義個個月的天數
Console.Write("請輸入月份:");
int yf = int.Parse(Console.ReadLine());
Console.Write("請輸入幾號:");
int h = int.Parse(Console.ReadLine());//輸入日期,賦值到h,
switch (yf)//排列每個月份,
{
        case 1:
               Console.WriteLine("第" + h.ToString() + "天");//一月輸入幾號,表示這是這一年的第幾天,直接用h
               break;//結束

       case 2:
               Console.WriteLine("第" + (m1 + h).ToString() + "天");//二月的多少號(h),加上一月的天數(m1=31),就是這一年的第幾天
              break;

      //下面以此類推
      case 3:
               Console.WriteLine("第" + (m1 + m2 + h).ToString() + "天");
               break;

      case 4:
               Console.WriteLine("第" + (m1 + m2 + m3 + h).ToString() + "天");
break;
      case 5:
               Console.WriteLine("第" + (m1 + m2 + m3 + m4 + h).ToString() + "天");
               break;
      case 6:
               Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + h).ToString() + "天");
               break;
      case 7:
               Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + h).ToString() + "天");
               break;
      case 8:
               Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + h).ToString() + "天");
               break;
      case 9:
               Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + h).ToString() + "天");
               break;
      case 10:
               Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + h).ToString() + "天");
               break;
     case 11:
               Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + h).ToString() + "天");
               break;
    case 12:
               Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + h).ToString() + "天");
              break;
              default://如果上麵條件都不滿足
              Console.WriteLine("您輸入的有誤!");
              break;
}

Console.ReadLine();

 

 

 

//輸入年,月,日,判斷格式是否正確
Console.Write("請輸入年份:");
int year = int.Parse(Console.ReadLine());
//判斷年份是否正確
if (year >= 0 && year <= 9999) //年份正確
{ //輸入月份
        Console.Write("請輸入月份:");
        int yf = int.Parse(Console.ReadLine());
        //判斷月份是否正確
        if (yf >= 1 && yf <= 12)//月份正確
       { //輸入日期
             Console.Write("請輸入日期:");
             int day = int.Parse(Console.ReadLine());
             //判斷日期輸入是否正確,首先判斷大小月,先排除二月
             //1.大,3.大,5.大,7.大,8.大,10.大,12.大;2,4,9,11小;2月特殊
            //如果此條件成立,那麼輸入的是大月
            if (yf == 1 || yf == 3 || yf == 5 || yf == 7 || yf == 8 || yf == 10 || yf == 12)
            {
                if (day > 0 && day <= 31)
                {
                       Console.WriteLine("日期輸入正確");
                       Console.WriteLine(year + "年" + yf + "月" + day + "日");
                }
               else
               {
                       Console.WriteLine("您輸入的日期有誤!");
               }
           }
           else if (yf == 4 || yf == 6 || yf == 9 || yf == 11)//輸入的是小月
           {
                  if (day > 0 && day <= 30) //日期輸入正確
                  {
                           Console.WriteLine("日期輸入正確");
                           Console.WriteLine(year + "年" + yf + "月" + day + "日");
                  }
                 else
                  {
                           Console.WriteLine("您輸入的日期有誤!");
                  }
           }
           else if (yf == 2) //輸入的是二月,需要判斷平年還是閏年
           {//首先判斷是否是閏年
            //年份可以被4整除,且不能被100整除
            //特殊年份(世紀年):可以被400整除
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) //是閏年
               {
                   if (day > 0 && day <= 29) //日期輸入正確
                  {
                          Console.WriteLine("日期輸入正確");
                          Console.WriteLine(year + "年" + yf + "月" + day + "日");
                          Console.WriteLine(year + "年是閏年");
                  }
                 else//日期輸入有誤
                 {
                         Console.WriteLine("您輸入的日期有誤!");
                 }
              }
              else//說明不是閏年
              {
                  if (day > 0 && day <= 28)//日期輸入正確
                 {
                        Console.WriteLine("日期輸入正確");
                        Console.WriteLine(year + "年" + yf + "月" + day + "日");
                        Console.WriteLine(year + "年不是閏年");
                  }
                 else//日期輸入有誤
                  {
                        Console.WriteLine("您輸入的日期有誤!");
                  }
              }

           }
       }
      else
      {
          Console.WriteLine("月份輸入有誤!");
       }
}
else//年份輸入有誤
{
Console.WriteLine("年份輸入有誤!");
}
Console.ReadLine();


條件陳述式練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.