国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

if---(switch-case)語句初步學(xué)習(xí)總結(jié)

2019-11-17 03:18:43
字體:
供稿:網(wǎng)友

if---(switch-case)語句初步學(xué)習(xí)總結(jié)

Daily sentence: Happiness is about having each tiny wish come true. 幸福就是達(dá)成每一個(gè)Tiny Wish.

Ctrl+E D C#自動(dòng)排版.

強(qiáng)制轉(zhuǎn)換:

如果表達(dá)式中含有一個(gè)double類型的的操作數(shù)時(shí)候,整個(gè)表達(dá)式都提升為double類型.

int a=(int)3.14; 將3.14強(qiáng)制轉(zhuǎn)換為int類型.并將值賦給a.

int 變量Convert.ToInt32(Console.ReadLine(輸入的字符串)); 將輸入的字符串轉(zhuǎn)換為int類型.

Convert轉(zhuǎn)換不再僅是內(nèi)存級(jí)別的轉(zhuǎn)換,而是考慮數(shù)據(jù)意義的轉(zhuǎn)換.Convert是一個(gè)加工轉(zhuǎn)換的過程.(要明白為什么轉(zhuǎn)換?!)Convert.ToInt32();Convert.ToString(); (一切類型都可以轉(zhuǎn)換成string類型)

int a = 10;Console.WriteLine(a.ToString()); 將int轉(zhuǎn)換為string類型.

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 試算{    class PRogram    {        static void Main(string[] args)        {            int a = 11111;                      Console.WriteLine(a.ToString());//將int類型的a的值11111轉(zhuǎn)換為string類型"11111".            int age = 23;            Console.WriteLine(age.ToString());//將int類型的值轉(zhuǎn)換為string類型.            Console.WriteLine("請(qǐng)輸入number的值?");            int number = Convert.ToInt32(Console.ReadLine());//將輸入的字符串轉(zhuǎn)換為int類型.            Console.WriteLine("a的值是:{0}  number的值是:{1}  你的年齡是:{2}",a,number,age);            Console.ReadKey();        }    }}
View Code

邏輯與邏輯或的短路

邏輯與的短路:當(dāng)?shù)谝粋€(gè)表達(dá)式不成立時(shí),就不再執(zhí)行后面的表達(dá)式.

int a=10;

int b=15;

bool result= ++a>15 &&++b>10;

Console.WriteLine("a的值為:{0} b的值為:{1}",a,b );

當(dāng)執(zhí)行邏輯與(&&)時(shí)第一個(gè)bool表達(dá)式++a>15不成立時(shí),后面的++b>10就不執(zhí)行直接跳過,最后a的值為11,b的值仍為15.

只有當(dāng)++a>15成立時(shí),才會(huì)運(yùn)行++b>15.最后a和b的值都加1.即a=11,b=16.

邏輯或的短路:當(dāng)?shù)谝粋€(gè)表達(dá)式成立時(shí),就不再運(yùn)行后面的表達(dá)式了.(||只要有一個(gè)成立即可)

int a=10;

int b=15;

bool result= ++a<15 ||++b>10;

Console.WriteLine("a的值為:{0} b的值為:{1}",a,b );

因?yàn)?+a<15成立.后面的++b>10就不再執(zhí)行,最后a的值為11,b的值仍為15.

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 邏輯與或的短路{    class Program    {        static void Main(string[] args)        {            //邏輯或的短路            //int a = 10;            //int b = 15;            //bool result = ++a < 15 || ++b > 10;            //Console.WriteLine("a的值為:{0} b的值為:{1}", a, b);            //邏輯與的短路            int a = 10;            int b = 15;            bool result = ++a > 15 && ++b > 10;            Console.WriteLine("a的值為:{0} b的值為:{1}", a, b);                        Console.ReadKey();        }    }}
View Code

if結(jié)構(gòu)(if/if-else/if-else if)

多練習(xí)熟練如何才能讓程序更加優(yōu)化,在使用if語句時(shí)上面三者的選擇問題.

if結(jié)構(gòu)(else永遠(yuǎn)和最近的if配對(duì)) if(bool表達(dá)式) ----必須是bool表達(dá)式 {語句1 }

if-else結(jié)構(gòu)if(條件){語句1;}else{語句2;}

if-else if結(jié)構(gòu)(只有當(dāng)if中的不成立才會(huì)進(jìn)入else if中進(jìn)行判斷)

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 密碼提示問題{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("請(qǐng)輸入你的密碼?");            string secret = Console.ReadLine();            if (secret == "888888")            {                Console.WriteLine("你輸入的密碼正確!");            }            else             {                Console.WriteLine("請(qǐng)重新輸入密碼?");                secret = Console.ReadLine();                if (secret == "888888")                {                                       Console.WriteLine("密碼正確");                }                else                 {                    Console.WriteLine("密碼錯(cuò)誤");                }            }            Console.ReadKey();        }    }}
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace if_else_if結(jié)構(gòu){    class Program    {        static void Main(string[] args)        {            Console.WriteLine("請(qǐng)輸入一個(gè)數(shù)字?");            int number = Convert.ToInt32(Console.ReadLine());            if (number >= 90)            {                Console.WriteLine("A");            }            else if (number >= 80)            {                Console.WriteLine("B");            }            else if (number >= 70)            {                Console.WriteLine("C");            }            else if (number >= 60)            {                Console.WriteLine("D");            }            else            {                Console.WriteLine("E");            }            Console.ReadKey();        }    }}
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 作業(yè){    class Program    {        static void Main(string[] args)        {            //作業(yè)1                        Console.WriteLine("請(qǐng)輸入你的用戶名?");            string useName = Console.ReadLine();            Console.WriteLine("請(qǐng)輸入你的密碼?");            string useSecret = Console.ReadLine();                                   if (useName == "admin" && useSecret == "mypass")                    {                        Console.WriteLine("登錄成功!");                    }                                        Console.ReadKey();        }    }}
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace if結(jié)構(gòu){    class Program    {        static void Main(string[] args)        {                       Console.WriteLine("請(qǐng)輸入你的年齡!");           int age=Convert.ToInt32( Console.ReadLine());           if (age >=18)               Console.WriteLine("你已經(jīng)成年.");           else               Console.WriteLine("你未成年.");            Console.ReadKey();                                                                }    }}
View Code
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 自治县| 彰武县| 巴马| 岳普湖县| 刚察县| 外汇| 江津市| 东乡族自治县| 漾濞| 来宾市| 漠河县| 温州市| 杨浦区| 罗平县| 邢台县| 翁牛特旗| 雷山县| 南雄市| 贺州市| 阿瓦提县| 隆化县| 吴江市| 卢龙县| 阜平县| 天水市| 宁武县| 班玛县| 鹤岗市| 平南县| 托里县| 开化县| 江安县| 抚宁县| 太白县| 清徐县| 仙居县| 施秉县| 鹿邑县| 芜湖市| 肥东县| 舟山市|