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. OpenRisc-48-or1200的SPRS模块分析

    引言 之前,我们在分析or1200的WB模块时(http://blog.csdn.net/rill_zhen/article/details/10220619),介绍了OpenRISC的GPRS(ge ...

  2. Android 数字签名学习笔记

    Android 数字签名学习笔记 在Android系统中,所有安装到系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程序之间建立信任关系,如果一个permission的pro ...

  3. 简单QT界面信号图形化输入输出

    右键->转到槽,选择信号 就可以输入代码 右键->转到槽,选择信号 就可以输入代码 2个文本框接受输入数字,第3个文本框输出相加结果 void Dialog::on_pushButton_ ...

  4. Tkinter类之窗口部件类

    Tkinter类之窗口部件类 Tkinter支持15个核心的窗口部件,这个15个核心窗口部件类列表如下:窗口部件及说明:Button:一个简单的按钮,用来执行一个命令或别的操作.Canvas:组织图形 ...

  5. SQLite使用报告

    SQLite简介 SQLite是遵守ACID的关联式数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目. 不像常见的客户-服务器范例,SQLite引擎不是个程 ...

  6. 简单实现仿UITabBarController界面

    第一步:添加两个占位View 第二步:添加子控制器 第三步:添加按钮 #import "ViewController.h" #define SCREEN_WIDTH ([UIScr ...

  7. 使用ionic与cordova(phonegap)进行轻量级app开发前的环境配置与打包安卓apk过程记录

     前言 有人说:"如果你恨一个人,就让ta去接触cordova(phonegap)",这是因为这里面的水很深,坑很多,真让人不是一般地发狂.或许有幸运的人儿基本顺顺利利就配置完环境 ...

  8. spring框架详解

    把之前分享的spring框架整理一份放在这里. 整体架构: Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架 框架图(选自:http://docs.spring.io/spr ...

  9. js charts去掉logo

    打开js charts 3的源代码搜索关键字"fs.bg",然后会找到 fs.bg.2v(fX),将这句代码删掉就OK了,可能有的版本会是fs.bg.2u(fX) 欢迎加入群:25 ...

  10. 1.js编程风格。 --- 编写可维护的javascript

    1. 使用4个空格字符作为一个缩进层级. 2. 不省略分号. ---> 自动插入分号机制非常复杂,且难于记忆. 3. 行的长度限定于80个字符. 4. 通常在运算符换行之后,下一行会增加两个层级 ...