Java从零开始学七(选择结构)
一、 程序的结构:
一般来说程序的结构包含有下面三种:
二、顺序结构
程序至上而下逐行执行,一条语句执行完之后继续执行下一条语句,一直到程序的末尾
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAACECAIAAADwRmJLAAAI80lEQVR4nO2dP2jbTBiHb0whg8eOGd2mFA0OGLpoLHTxVAKlREupR28xHerRo6fiIYPo5NFDKR1FJtGhmBKCCSWYDMFDBhNCEaUEfYO+T5/+nhTfq5N0+j2TLcen9957nztJji3mAtBgWNkBAFAm/wtgWdZ4PB7VgfF4bFlWEenQNI0phKZpRWRJJZjruqvVqtvt6ro+HA7Lru1cDIdDXde73e5qtSJOB1NqSVSsO0XAHMfRNM227bIjeTC2bWua5jgOYZuKVYxi3SkCNp1O+/1+2WFsSb/fn06nhA0qVjGKdacIGHkNyYTcXsUqRrHuFAEzDMM0zbLD2BLTNA3DIGxQsYpRrDtFUHsB3rx5s9lsNpvN379/xRtUrGIYY5sAd3d3ZUdUOSQJECksqjozTfPVq1eWZVmWdXp6en19LdigHAGkacYYswIsFgs5+60RNAIkXoSO/EHiY0GCAnjc3t7mf/t6vY5sEYwt7Xp85i4Kyg8EyIRsBeAPYfxVkmGOC3BxcZH/7aPRSNO0+XyeGKcgaU3xJYkLIxgDBOAjQwC/4iOjKz7Spmm+ePFiEuDk5MTKjWEY/iemnga0xZe/y8FJAQLIpEABgrOaG9OAZKozTfPRo0dpc+pD8f4PQiSexFqP5yGegTySbBcPBOAj4xyApUxvJAI8ffr0KMD79+/z/0uFrut+tL1eb7FYUAkQrHU3vdcQoHSkHgK5BawAkXOAB43xaDTySz8x8ocSSUJiNyFApZB3ElzQCiAiwLdv3yJ/TyJA4gIYfIpDoOpAdgjEeermq4wtEBQgjnhI23kOAcqCQIC0cuc8rcghUBza4svTTZI8cBqHAHxq/68QlRWgCkCATCBACAjQNNh4PB4Oh2WHsSXHx8fv3r2DAGlAgEzYcrkk/16VHBzH2d/f//z5MwRIAwJkw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选择结构
3.1 、IF语句--单分支结构
示例一、 比较2个整型变量的大小
package pb.test; public class test4 { public static void main(String[] args) {
int x = 3;
int y = 10; //定义2个整型变量,x,y
System.out.println("=======开始比较=======");
if(x>y){
System.out.println("x比y大!");
}
if(x<y){
System.out.println("x小于y!");
}
if(x==y){
System.out.println("x等于y!");
}
System.out.println("=======比较完成======="); } }
3.2、if...else语句--双分支结构
示例二、判断一个数字是奇数还是偶数
package pb.test; public class test5 { public static void main(String[] args) {
int x=3; //定义大中型变量x
if(x%2==0){ //如果余数为0则是偶数
System.out.println("x是偶数");
}else{ //如果余数不为0则是奇数
System.out.println("x是奇数");
} } }
3.3、三目运算符
示例三、求出2个数字中的最大值
public static void main(String[] args) {
//定义变量来保存最大值
int max=0;
//定义2个变量
int x=3;
int y=10;
//使用3目运算判断如果x>y则,max=x,否则max=y;
max=x>y?x:y;
System.out.println("max为: "+max); }
3.4、if…else if…else语句 --多分支结构
示例四、
package pb.test; public class test6 { public static void main(String[] args) {
int x=3;
if(x==1){
System.out.println("x的值是1!");
}else if(x==2){
System.out.println("x的值是2!");
}else if(x==3){
System.out.println("x的值是3!");
}else{
System.out.println("x的值不是1,2,3 中的一个!");
} } }
四、Switch结构
4.1、执行流程
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaUAAADaCAIAAACJs8pZAAAUaElEQVR4nO2dvWtb1xvHTxqXCiocFUIRWeKhQ4YQ7mBChg7eagikakggm7UYZ9SUZAnSlm76Azxk6KChg4bgWYQMpnTQUIqGUoSGooIxxpgiigf9hlvu7+q+nHvueX/5foYiS/ec+zzPOfejx1dqTNYAKKbb7RJCJpOJ6UBA6BDTAQD/ge+AJcB3QDnwHbAE+A4oZDgckhSNRmO5XJoOCoQLfAcUslqt2u124rter2c6IhA08B1QS9LiobkDxoHvgFqSFg/NHTAOfAeUMxwO0dwBG4DvgHI+fvz47Nkz+A4YB74DSlgul+/fv3/x4kWr1Xr06FG3222321EUvXnz5vT01HR0IFDgOyCT09PTN2/eRFHUbre73e5oNLq4uEhenU6n7969e/ToUavVevHixfv379H0AZ3Ad0CUTCv37t276XRKH3JxcTEajdD0Ac3Ad4CT6XTa7/fLWrla86SbPu55AKgEvgM1yPRl/X6/spWrO3mtPhGAWsB3oBr9993o9wEB4AO+A8VYcouN4+YgAGXAd2ADmz9CRdMHBIHvgC2tHDto+gAf8F242NzKsYOmD7AD34WFc60cO2j6QCXwXRAoauUIKd0/+ZcoB3McRgdNHygEvvMWRa0coZI5kvIjZX72gytB0wfSwHe+ofOuHN1KIr5jP54dNH0AvvMBzXflWJo7loPprSJlTkHQ9AULfOc2k8nE4AeslY5jOZhxTkWkm76XL1+uViudZweage/cZjqd6vywtVZHxuK7wqZPaQppMo3ecDjUdmpgBPjOE/R/mS6jp7yn8u6j9IOFjxWBG3nBAt/5hp57eRnNlfVuZcezHCmXuJV7/vz59vb2gwcPDg8Pf/rppz/++OP8/FzF6YCdwHc+Ezd9Dx8+vHXr1vPnzyV+7Y7FUGWOo3eClAk5OD09ff369f3792/fvv348eO3b99++PBhssnHjx9/++23v/76CzfvvAe+85N///13uVz+/vvvnz59mkwmHz58ePv27ePHj2/fvn3//v3Xr1/zNX1ld9koXVul3ZJp85PziS/fyh0fH0/Y+OWXX9D0eQx85xvX19ez2YxySR8fHx8eHj548GB7e5u76UvElHZT5sd1lbAKP+VgH56BpZVj59OnTy7+38SADnznJ5n+rhCRpo8iqXXufhzlY1yK0QpPkUGklSsE/Z3fwHf+c3l5+eeff/7666+U61xK06cNua0c7t+FA3wXEKqbPqWglQPiwHeB4krTF7dy9+7dQysHxIHvQsfCpi9u5X744Ydms/nNN98cHh6+fPkyjmQwGHBoDq0ciIHvwP8x2/Sdnp6+evXq3r17X3311cOHDzOtXGw6dt+hlQN54DtQAEfT9+rVK46mL93KxXflklYuz2AwqPQdWjlAAb4DFaho+vABKzACfAdYEbzThw9YgXHgO8BDrabviy++2N7ebrVaaOWAWeA7IARL03fz5k1CyLfffsutObRyQArwHZBGWdO3t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u4DvwAbwHfAY+A5sAN8Bj4HvwAbwHfAY+A5s8M8//1x4hOlyAruA7wAAoQDfAQBC4X+D0rvUo3vhZwAAAABJRU5ErkJggg==" alt="" />
示例五、判断学生成绩,并给于评分,90~100分,A级大于80分,B级,大于70分,输出C级,大于60分,D级,低于60分输出E级
package pb.test; public class test7 { public static void main(String[] args) {
int score = 75; //声明学生分数
switch (score/10){
case 10:
case 9:
System.out.println("A级");
break;
case 8:
System.out.println("B级");
break;
case 7:
System.out.println("C级");
break;
case 6:
System.out.println("D级");
break;
default:
System.out.println("E级");
break;
} } }
结果:C级
还是上面的如果不加break;
package pb.test; public class test7 { public static void main(String[] args) {
int score = 75; //声明学生分数
switch (score/10){
case 10:
case 9:
System.out.println("A级"); case 8:
System.out.println("B级"); case 7:
System.out.println("C级"); case 6:
System.out.println("D级"); default:
System.out.println("E级"); } } }
结果:C级,D级,E级
Java从零开始学七(选择结构)的更多相关文章
- Java从零开始学八(循环结构)
一.循环结构 循环结构则是根据判断条件的成立与否,决定程序段落的执行次数,而这个程序段落就称为循环主体.
- 038 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 05 案例演示switch结构-星期的表示案例以及总结
038 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 05 案例演示switch结构-星期的表示案例以及总结 本文知识点:案例演示switch结构并对sw ...
- 034 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 01 流程控制概述
034 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 01 流程控制概述 本文知识点:Java中的流程控制相关概念的认识 三大流程控制语句结构的简介 顺序 ...
- Java 第三章 选择结构1
选择结构(一) 会使用基本的 if 选择结构 掌握逻辑运算符,掌握多重 if 选择结构 , 掌握嵌套 if 选择 结构 为什么需要 if 选择结构 例如: 如果张浩的 java 考试成绩大于 98分, ...
- 037 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 04 switch结构
037 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 04 switch结构 本文知识点:Java中的switch结构 选择结构分类 选择结构只有如下2种 ...
- 036 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 03 嵌套if结构
036 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 03 嵌套if结构 本文知识点:Java中的嵌套if结构 什么是嵌套if结构? 概念: 嵌套if结构 ...
- 035 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 02 多重if结构
035 01 Android 零基础入门 01 Java基础语法 04 Java流程控制之选择结构 02 多重if结构 本文知识点:Java中的多重if结构 选择结构回顾 if选择结构 注意: 1.条 ...
- java中的if-Switch选择结构
字随笔走,笔随心走,随笔,随心.纯属个人学习分析总结,如有观者还请不啬领教. 1.if选择结构 什么是if结构:if选择结构是根据判断结果再做处理的一种语法结构. 起语法是: if(判断条件){ 操作 ...
- 小白的java学习之路 “ 选择结构(一)”
if选择结构: if选择结构是根据条件判断之后再做处理的一种语法结构. 1.if选择结构的语法: public class Demo{ public static void main(String[] ...
随机推荐
- spring---transaction(1)---源代码分析(事务的拦截器TransactionInterceptor)
写在前面: 先了解一下spring的事务.分为分明式事务管理和注解式事务管理,对于前期的事务,spring会通过扫描拦截对于事务的方法进行增强(以后讲解). 若果目标方法存在事务,spring产出的b ...
- Maven项目META-INF文件夹不存在的问题
直接在resources文件夹下新建META-INF文件夹即可. 参考: https://my.oschina.net/KingPan/blog/750159 https://stackoverflo ...
- Linux服务器压测/拷机软件收集
最近公司采购了一批服务器,于是收集了一些拷机软件来压测服务器硬件性能.硬件的稳定相对来说比较重要,7x24小时无间断运行,主要看三个硬件:CPU.内存.硬盘. 下面是收集的一些教程,可能网址已经失效了 ...
- Design Pattern Singleton 单一模式
单一模式的几个注意点: 一) 设计单一模式,首先须要把构造函数给私有化了,不让外界訪问,那么外界仅仅能通过提供的函数获取一个新的类. 二) C++的单一模式,记得要在类外初始化一个类,否则或内存出错的 ...
- 基于tomcat7 web开发中的一点小东西
控制台: org.apache.jasper.compiler.TldLocationsCache tldScanJar 信息: At least one JAR was scanned for TL ...
- hue解决timed out(code THRIFTSOCKET):None
报错栈: Traceback (most recent call last): File , in decorator return func(*args, **kwargs) File , in e ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第14章节--使用Office Services开发应用程序 总结
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第14章节--使用Office Services开发应用程序 总结 SP2013新的server端Off ...
- Objective-C:NSMutableString类的常见操作
NSMutableString可变字符串的主要的操作是创建.增加.删除.插入.替换 代码操作如下: // // main.m // 03-NSMutableString // // Created b ...
- hdu 1242 Rescue(BFS入门)
第一次用容器做的BFS题目,题目有个地方比较坑,就是遍历时的方向,比如上下左右能AC,右上左下就WA #include <stdio.h> #include <string.h> ...
- otl中遇到的一些字符数组长度问题
最近做的项目中对于数据库的操作为了有一个统一的操作,所以采用了otl技术作为中间媒介,当然,由于是用了别人的库,所以还是出现了很多问题.通过对问题进行分析,也形成了一些常用问题的处理方法. 在用otl ...