if---(switch-case)语句初步学习总结
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)语句初步学习总结的更多相关文章
- while, do-while ,switch···case语句的学习与运用
1.while语句:当···的时候 格式:初始条件 while(循环条件) { 循环体; 状态改变; } 相当于 ...
- 为什么说在使用多条件判断时switch case语句比if语句效率高?
在学习JavaScript中的if控制语句和switch控制语句的时候,提到了使用多条件判断时switch case语句比if语句效率高,但是身为小白的我并没有在代码中看出有什么不同.去度娘找了半个小 ...
- Java switch case语句
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. switch case 语句语法格式如下: switch(expression){ case value : ...
- java中的Switch case语句
java中的Switch case 语句 在Switch语句中有4个关键字:switch,case break,default. 在switch(变量),变量只能是整型或者字符型,程序先读出这个变量的 ...
- switch… case 语句的用法
switch… case 语句的用法 public class Test7 { public static void main(String[] args) { int i=5; switch(i ...
- if语句,if...else if语句和switch...case语句的区别和分析
前段时间在工作中遇到了一个关于条件判断语句的问题,在if语句,if else if语句和switch case语句这三者之间分析,使用其中最有效率的一种方法. 所以就将这个问题作为自己第一篇博客的主要 ...
- Python | 基础系列 · Python为什么没有switch/case语句?
与我之前使用的所有语言都不同,Python没有switch/case语句.为了达到这种分支语句的效果,一般方法是使用字典映射: def numbers_to_strings(argument): sw ...
- 为什么switch...case语句比if...else执行效率高
在C语言中,教科书告诉我们switch...case...语句比if...else if...else执行效率要高,但这到底是为什么呢?本文尝试从汇编的角度予以分析并揭晓其中的奥秘. 第一步,写一个d ...
- JavaScript基础知识(if、if else、else if、while、switch...case语句)
13.语句 概念:就是分号(:) 代表一条语句的结束 习惯:一行只编写一条语句:一行编写多条语句(代码可读性较差) 语句块:可以包含多条语句 "{ }"将多条语句包裹 u ...
- C语言中switch case语句可变参实现方法(case 参数 空格...空格 参数 :)
正常情况下,switch case语句是这么写的: : : ... ;break ; default : ... ;break ; } 接下来说一种不常见的,但是对于多参数有很大的帮助的写法: 先给一 ...
随机推荐
- 使用keil判断ARM的冷启动和热启动的方法
微处理器:LPC2114 编译环境:Keil MDK V4.10 思路: 常把单片机系统的复位分为冷启动和热启动.所谓冷启动,也就是一般所说的上电复位,冷启动后片内外RAM的内容是随机的,通常是0x0 ...
- top k 算法
对于一个非有序的数组A[p..r],求数组中第k小的元素. 如何考虑 排序(部分排序)就不用说了..o(nlgn),当然如果在实际情况中要一直取值,当然要排序后,一次搞定,以后都是O(1) 我们这里提 ...
- Android APN配置
APN概念 APN(Access Point Name),即“接入点名称”,用来标识GPRS的业务种类,目前分为两大类:CMWAP(通过GPRS访问WAP业务).CMNET(除了WAP以外的服务目前都 ...
- Android 打包签名 从生成keystore到完成签名
进入生成工具: 工具帮助: 输入指令并获得结果: 转自: http://www.cppblog.com/fwxjj/archive/2010/05/24/116208.html 首先,我们需 ...
- 编程内功修炼之数据结构—BTree(三)总结
BTree必须通过各种编程约束,使得不脱离BTree的本身特性: 1)BTree关键字插入操作:插入过程中,如果节点关键字达到上限,添加分裂约束,从而控制每个节点的关键字数维持在 t-1~2*t-1内 ...
- java——推断日期是否在今天之前
这里说的日期是指字符串的日期格式,如"2014-10-15",我们要推断这个日期是否在今天之前,网上看到好多推断的方法,都是拿这个日期转换成Date对象 然后与new Date() ...
- [ACM] hdu 1003 Max Sum(最大子段和模型)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- Android GridView(九宫图)
GridView跟ListView都是比较常用的多控件布局,而GridView更是实现九宫图的首选! <?xml version="1.0" encoding="u ...
- Docker容器的数据管理
Docker容器的数据管理 Docker容器的数据管理 什么是数据卷(Data Volume)? 数据卷是经过特殊设计的目录,可以绕过联合文件系统(UFS),为一个或者多个容器提供访问 数据卷设计的目 ...
- 在IE6/7下表格td标签没有内容时不显示边框?
有以下几种方法: 1.在单元格中加入一个空格.这样: <td> </td> 2.直接在table里这样写:<table border="0" cell ...