Daily sentence:  Happiness is about having each tiny wish come true. 幸福就是达成每一个Tiny Wish.

Ctrl+E D C#自动排版.

强制转换:

如果表达式中含有一个double类型的的操作数时候,整个表达式都提升为double类型.

int a=(int)3.14; 将3.14强制转换为int类型.并将值赋给a.

int 变量Convert.ToInt32(Console.ReadLine(输入的字符串)); 将输入的字符串转换为int类型.

Convert转换不再仅是内存级别的转换,而是考虑数据意义的转换.Convert是一个加工转换的过程.
(要明白为什么转换?!)
Convert.ToInt32();
Convert.ToString(); (一切类型都可以转换成string类型)

int a = 10;
Console.WriteLine(a.ToString()); 将int转换为string类型.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 试算
{
class Program
{
static void Main(string[] args)
{
int a = ;
Console.WriteLine(a.ToString());//将int类型的a的值11111转换为string类型"11111".
int age = ;
Console.WriteLine(age.ToString());//将int类型的值转换为string类型.
Console.WriteLine("请输入number的值?");
int number = Convert.ToInt32(Console.ReadLine());//将输入的字符串转换为int类型.
Console.WriteLine("a的值是:{0} number的值是:{1} 你的年龄是:{2}",a,number,age); Console.ReadKey();
}
}
}

逻辑与逻辑或的短路

逻辑与的短路:当第一个表达式不成立时,就不再执行后面的表达式.

int a=10;

int b=15;

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

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

当执行逻辑与(&&)时第一个bool表达式++a>15不成立时,后面的++b>10就不执行直接跳过,最后a的值为11,b的值仍为15.

只有当++a>15成立时,才会运行++b>15.最后a和b的值都加1.即a=11,b=16.

逻辑或的短路:当第一个表达式成立时,就不再运行后面的表达式了.(||只要有一个成立即可)

int a=10;

int b=15;

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

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

因为++a<15成立.后面的++b>10就不再执行,最后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 = ;
int b = ;
bool result = ++a > && ++b > ; Console.WriteLine("a的值为:{0} b的值为:{1}", a, b); Console.ReadKey(); }
}
}

if结构(if/if-else/if-else if)

多练习熟练如何才能让程序更加优化,在使用if语句时上面三者的选择问题.

if结构(else永远和最近的if配对)

if(bool表达式) ----必须是bool表达式
{
语句1
}

if-else结构
if(条件)
{语句1;}
else
{语句2;}

if-else if结构(只有当if中的不成立才会进入else if中进行判断)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 密码提示问题
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入你的密码?");
string secret = Console.ReadLine();
if (secret == "")
{
Console.WriteLine("你输入的密码正确!");
}
else
{
Console.WriteLine("请重新输入密码?");
secret = Console.ReadLine();
if (secret == "")
{ Console.WriteLine("密码正确");
}
else
{
Console.WriteLine("密码错误");
}
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace if_else_if结构
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个数字?");
int number = Convert.ToInt32(Console.ReadLine());
if (number >= )
{
Console.WriteLine("A");
}
else if (number >= )
{
Console.WriteLine("B");
}
else if (number >= )
{
Console.WriteLine("C");
}
else if (number >= )
{
Console.WriteLine("D");
}
else
{
Console.WriteLine("E");
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 作业
{
class Program
{
static void Main(string[] args)
{
//作业1 Console.WriteLine("请输入你的用户名?");
string useName = Console.ReadLine();
Console.WriteLine("请输入你的密码?");
string useSecret = Console.ReadLine(); if (useName == "admin" && useSecret == "mypass")
{
Console.WriteLine("登录成功!");
} Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace if结构
{
class Program
{
static void Main(string[] args)
{ Console.WriteLine("请输入你的年龄!");
int age=Convert.ToInt32( Console.ReadLine());
if (age >=)
Console.WriteLine("你已经成年.");
else
Console.WriteLine("你未成年.");
Console.ReadKey(); }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace if_else_if_作业
{
class Program
{
static void Main(string[] args)
{
//作业1
//Console.WriteLine("请输入你的密码?");
//string secret = Console.ReadLine();
//if (secret == "888888")
//{
// Console.WriteLine("你输入的密码正确!");
//}
//else
//{
// Console.WriteLine("密码错误,请重新输入!");
//} //secret = Console.ReadLine();
//if (secret == "888888")
//{
// Console.WriteLine("你输入的密码正确!");
//}
//else
//{
// Console.WriteLine("密码错误,请重新输入!");
//} //作业1(优化方法---可以将if-else直接嵌套进else中)
//Console.WriteLine("请输入你的密码?");
//string secret = Console.ReadLine();
//if (secret == "888888")
//{
// Console.WriteLine("你输入的密码正确!");
//}
//else
//{
// Console.WriteLine("请重新输入密码?");
// secret = Console.ReadLine();
// if (secret == "888888")
// { // Console.WriteLine("密码正确");
// }
// else
// {
// Console.WriteLine("密码错误");
// }
//} ////作业2
//Console.WriteLine("请输入你的用户名?");
//string useName = Console.ReadLine();
//Console.WriteLine("请输入你的密码?");
//string secret = Console.ReadLine();
//if (useName == "admin" && secret == "888888")
//{
// Console.WriteLine("你输入的用户名和密码正确!");
//}
//else if (useName != "admin" /*|| secret == "888888"*/)
//{
// Console.WriteLine("你输入的用户名不存在!");
//}
//else//(useName == "admin" || secret != "888888") 除去上面两种可能剩下的就是括号中的这种了.
//{
// Console.WriteLine("你输入的密码不正确!");
//} //作业3
Console.WriteLine("请输入你的年龄?");
int age = Convert.ToInt32(Console.ReadLine());
if (age >= )
{
Console.WriteLine("可以观看.");
}
//if (age < 10)
//{
// Console.WriteLine("不可以观看.");
//}
else if (age>=)
{
Console.WriteLine("是否决定观看?");
Console.WriteLine("请输入yes或者no?");
string input = Console.ReadLine();
if (input == "yes")
{
Console.WriteLine("请查看.");
} else
{
Console.WriteLine("退出,你放弃查看.");
} }
else
{
Console.WriteLine("不可以观看!");
}
Console.ReadKey();
}
}
}

****(还需深入理解并会应用)

控制一些语句的是否输出:应用bool表达式加上if结构完成.

bool 变量=false;

if(变量==false)

{

要控制是否输出的程序.

}

****

switch-case语句

可以解决一些if语句计较繁琐的代码.

结构为:

switch(表达式)

  case:"值1":

    语句块1;

  break;

......

  case:"值n":

    语句块n;

  break;

  default:语句块;

  break;

上面的语法中:break是每句都需要写的.表达式中的值与case中的值一个一个比较,直到找到相等的为止,如果没有就将跳入default语句中,执行default语句中的程序,

如果没有default语句,程序就直接跳出.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace switch_case
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个字母(A-E)?");
string input = Console.ReadLine();
int salary = ;
bool sign = false;
switch(input)
{
case "A":
salary += ;
break;
case"B":
salary += ;
break;
case"C":
break;
case"D":
salary -= ;
break;
case"E":
salary -= ;
break; default:
Console.WriteLine("你输入的英文字母有错误!");
sign = true;
break; }
if (sign == false)
{
Console.WriteLine("你的工资是" + salary);
} Console.ReadKey(); }
}
}

Long way to go......

if---(switch-case)语句初步学习总结的更多相关文章

  1. while, do-while ,switch···case语句的学习与运用

    1.while语句:当···的时候 格式:初始条件           while(循环条件)         {          循环体;          状态改变;         } 相当于 ...

  2. 为什么说在使用多条件判断时switch case语句比if语句效率高?

    在学习JavaScript中的if控制语句和switch控制语句的时候,提到了使用多条件判断时switch case语句比if语句效率高,但是身为小白的我并没有在代码中看出有什么不同.去度娘找了半个小 ...

  3. Java switch case语句

    switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. switch case 语句语法格式如下: switch(expression){ case value : ...

  4. java中的Switch case语句

    java中的Switch case 语句 在Switch语句中有4个关键字:switch,case break,default. 在switch(变量),变量只能是整型或者字符型,程序先读出这个变量的 ...

  5. switch… case 语句的用法

    switch… case 语句的用法   public class Test7 { public static void main(String[] args) { int i=5; switch(i ...

  6. if语句,if...else if语句和switch...case语句的区别和分析

    前段时间在工作中遇到了一个关于条件判断语句的问题,在if语句,if else if语句和switch case语句这三者之间分析,使用其中最有效率的一种方法. 所以就将这个问题作为自己第一篇博客的主要 ...

  7. Python | 基础系列 · Python为什么没有switch/case语句?

    与我之前使用的所有语言都不同,Python没有switch/case语句.为了达到这种分支语句的效果,一般方法是使用字典映射: def numbers_to_strings(argument): sw ...

  8. 为什么switch...case语句比if...else执行效率高

    在C语言中,教科书告诉我们switch...case...语句比if...else if...else执行效率要高,但这到底是为什么呢?本文尝试从汇编的角度予以分析并揭晓其中的奥秘. 第一步,写一个d ...

  9. JavaScript基础知识(if、if else、else if、while、switch...case语句)

    13.语句 概念:就是分号(:) 代表一条语句的结束 习惯:一行只编写一条语句:一行编写多条语句(代码可读性较差) 语句块:可以包含多条语句     "{ }"将多条语句包裹 u ...

  10. C语言中switch case语句可变参实现方法(case 参数 空格...空格 参数 :)

    正常情况下,switch case语句是这么写的: : : ... ;break ; default : ... ;break ; } 接下来说一种不常见的,但是对于多参数有很大的帮助的写法: 先给一 ...

随机推荐

  1. 不要在公共接口中传递STL容器

    最近的一个项目,是开发一个framework,提供给公司内部不同的产品线使用. 之间遇到的一个问题,就是STL容器的使用, 而结论是不要在公共接口中传递STL容器: 这里说的STL容器,但主要则是指容 ...

  2. 普林斯顿大学算法课 Algorithm Part I 学习资源

    网友笔记参考 果壳Mooc首页 revilwang的专栏 白色咖啡 Weiran Liu的渣技术小专栏 Bug表:http://findbugs.sourceforge.net/bugDescript ...

  3. 防止 NSTimer retain 作为 target 的 self

    先吐槽一下这个标题,空格略蛋疼,不像中文,但是不写空格看上去则更诡异,求解决方案…… NSTimer会retain它的target,这样如果在控制器当中定义一个NSTimer,target指定为sel ...

  4. wx.Dialog

    wx.Dialog A dialog box is a window with a title bar and sometimes a system menu, which can be moved ...

  5. 如何修改 UINavigationController、UINavigationBar 中 navigationItem 左侧 “返回” 按钮的名称

    如果我们从 title 为 “首页” 的页面 A 点击进入一个子页面 B,那么在页面 B 的左上角将显示一个名为 “<首页” 的按钮.假设这个页面 A 叫 “你是我天边最美的云彩”,那在页面 B ...

  6. Nodejs随笔(一):Hello World!

    声明:本人用的是Ubuntu 14.04 LTS 系统. 一.Nodejs安装: <1>直接apt-get安装,最简单:sudo apt-get install nodejs <2& ...

  7. Thinkphp利用微信多客服消息推送取货二维码消息

    首先看微信官方的说法: 当用户主动发消息给公众号的时候(包括发送信息.点击自定义菜单.订阅事件.扫描二维码事件.支付成功事件.用户维权), 微信将会把消息数据推送给开发者,开发者在一段时间内(目前修改 ...

  8. EC读书笔记系列之14:条款26、27、28、29、30、31

    条款26 尽可能延后变量定义式的出现时间(Lazy evaluation) 记住: ★尽可能延后变量定义式的出现.这样做可增加程序的清晰度并改善程序效率 ----------------------- ...

  9. stat 函数讲解

    表头文件:    #include <sys/stat.h>                     #include <unistd.h>定义函数:    int stat( ...

  10. 如何在MyEclipse中部署struts2的环境

    总记不住一些部署struts2框架的细节,下面就做一个总结:其实很简单,只要几步:1.下载的strutsXXX(版本号)-zip文件中解压app目录中有一个struts2-blank.war文件,解压 ...