当前位置: 首页 > news >正文

青岛响应式网站谷歌自然排名优化

青岛响应式网站,谷歌自然排名优化,知识产权网站建设,四川手机网站建设公司Java 条件语句概述 条件语句通过判断给定条件的真假来控制程序的执行。本小节将详细介绍 Java 中各类条件语句。 1. if 语句 1.1 语法 用于根据给定条件决定是否执行一段代码。if 块仅在关联的布尔表达式为 true 时执行。 if (条件) {// 当条件成立时执行此处代码 }大括号…

Java 条件语句概述

条件语句通过判断给定条件的真假来控制程序的执行。本小节将详细介绍 Java 中各类条件语句。

1. if 语句

1.1 语法

用于根据给定条件决定是否执行一段代码。if 块仅在关联的布尔表达式为 true 时执行。

if (条件) {// 当条件成立时执行此处代码
}

大括号内的内容为语句块。

1.2 实例
public class IfStatement1 {public static void main(String args[]) {int age = 18;if (age >= 18) {System.out.println("在中国你已经成年");}}
}

age >= 18true 时,执行 if 语句块。若语句块内只有一条语句,可省略花括号,但不推荐。

2. if … else … 语句

2.1 语法

结合 else 使用,当布尔表达式为 false 时,执行 else 语句块。

if (条件) {// 如果条件成立,执行此处代码
} else {// 如果条件不成立,执行此处代码
}
2.2 实例
public class IfElseStatement1 {public static void main(String args[]) {int age = 15;if (age >= 18) {System.out.println("在中国你已经成年");} else {System.out.println("在中国你还未成年");}}
}

age >= 18false 时,执行 else 语句块。

补充

可使用条件表达式(三目运算符)表达式1? 表达式2 : 表达式3 简化 if else 语句。

public class IfElseStatement2 {public static void main(String args[]) {int age = 15;System.out.println(age >= 18? "在中国你已经成年" : "在中国你还未成年");}
}

3. if … else if … else 语句

3.1 语法

结合 else if 实现更复杂的程序分支结构。

if (条件1) {// 如果条件1成立,执行此处代码
} else if (条件2) {// 如果条件1不成立,并且条件2成立,执行此处代码
} else {// 如果条件1、条件2都不成立,执行此处代码
}
3.2 实例
// 根据给定分数向屏幕打印评级
public class IfElseIfStatement {public static void main(String args[]) {int score = 70;if (score >= 90) {System.out.println("优秀");} else if (score >= 70) {System.out.println("良好");} else if (score >= 60) {System.out.println("及格");} else {System.out.println("不及格");}}
}

程序遇到符合条件的分支就执行该分支语句块,不再执行其他分支。

3.3 嵌套 if … else 语句

可在 ifelse if 语句中嵌套使用 ifelse if 语句。

public class IfElseStatement1 {public static void main(String[] args) {int age = 25;int sex = 1;  // 此处用sex变量表示性别,1:男  2:女if (age >= 20) {System.out.println("在中国你已经成年");if (sex == 2) {System.out.println("并且到了法定的结婚年龄");}if (sex == 1 && age >= 22) {System.out.println("并且到了法定的结婚年龄");}} else {System.out.println("在中国你还未成年");}}
}

4. switch 语句

4.1 语法

可理解为简写版的多个 if.. else 语句。

switch () {case1:语句1.1...语句n.1break;case2:语句2.1...语句2.nbreak;default:语句n.1...语句n.n
}

规则:

  • switch 语句中的变量类型可以是 byteshortintchar 或者 String
  • 可拥有多个 case 语句,每个 case 后跟要比较的值和冒号。
  • case 语句中的值数据类型必须与变量相同,且只能是常量或字面常量。
  • 变量值与 case 语句值相等时,执行该 case 后的语句,直到 break 跳出。
  • 若无 break,程序继续执行下一条 case 语句,直到出现 break
  • 可包含 default 分支,一般为最后一个分支,无匹配时执行,无需 break
4.2 实例
public class SwitchStatement1 {public static void main(String args[]) {int i = 2;switch (i) {case 1:System.out.println("i的值为1");break;case 2:System.out.println("i的值为2");break;default:System.out.println("i的值既不等于1,也不等于2");}}
}

从 JDK5 开始,可与枚举值一起使用;从 JDK8 开始,可与 String 值一起使用。

5. 小结

Java 中条件语句主要有 if 语句和 switch 语句。

  • 场景选择:多个 == 判断用 switch 语句更清晰;复杂条件表达式用 if 语句更合适。
  • if 语句使用建议
    • 每个分支用 {} 括起来。
    • 多个 if... else 时,将可能性大的分支排在前面。
    • 避免 if... else 嵌套层级过深。
  • switch 语句使用建议
    • 每个分支不要漏写 break
    • 总是写上 default 分支。

文章转载自:
http://catacoustics.bbrf.cn
http://stakeout.bbrf.cn
http://irresolvable.bbrf.cn
http://clepsydra.bbrf.cn
http://supremum.bbrf.cn
http://evalina.bbrf.cn
http://tintinnabulous.bbrf.cn
http://wear.bbrf.cn
http://untwist.bbrf.cn
http://aldebaran.bbrf.cn
http://increment.bbrf.cn
http://spontoon.bbrf.cn
http://rifter.bbrf.cn
http://bonza.bbrf.cn
http://priestess.bbrf.cn
http://oktastylos.bbrf.cn
http://vyivgly.bbrf.cn
http://institute.bbrf.cn
http://pinaster.bbrf.cn
http://disrelish.bbrf.cn
http://jerky.bbrf.cn
http://afternoons.bbrf.cn
http://rectorship.bbrf.cn
http://volute.bbrf.cn
http://vietnamize.bbrf.cn
http://zymoscope.bbrf.cn
http://dumping.bbrf.cn
http://strac.bbrf.cn
http://lana.bbrf.cn
http://acidulate.bbrf.cn
http://notandum.bbrf.cn
http://membraniform.bbrf.cn
http://tapeta.bbrf.cn
http://babesia.bbrf.cn
http://pacuit.bbrf.cn
http://stereoscopic.bbrf.cn
http://bagasse.bbrf.cn
http://humic.bbrf.cn
http://adulthood.bbrf.cn
http://retiracy.bbrf.cn
http://quinism.bbrf.cn
http://hela.bbrf.cn
http://periocular.bbrf.cn
http://recuperation.bbrf.cn
http://excisionase.bbrf.cn
http://overlade.bbrf.cn
http://rancheria.bbrf.cn
http://humiture.bbrf.cn
http://polygamical.bbrf.cn
http://moralism.bbrf.cn
http://dispersible.bbrf.cn
http://curtly.bbrf.cn
http://suave.bbrf.cn
http://compulsive.bbrf.cn
http://garpike.bbrf.cn
http://biennially.bbrf.cn
http://oasis.bbrf.cn
http://assembly.bbrf.cn
http://hideous.bbrf.cn
http://methenamine.bbrf.cn
http://monostome.bbrf.cn
http://improviser.bbrf.cn
http://dampness.bbrf.cn
http://kodacolor.bbrf.cn
http://munga.bbrf.cn
http://mainmast.bbrf.cn
http://sliver.bbrf.cn
http://compassionate.bbrf.cn
http://apologia.bbrf.cn
http://chinatown.bbrf.cn
http://concordia.bbrf.cn
http://splintage.bbrf.cn
http://distich.bbrf.cn
http://prolongation.bbrf.cn
http://hornet.bbrf.cn
http://douche.bbrf.cn
http://dockwalloper.bbrf.cn
http://superactinide.bbrf.cn
http://scribble.bbrf.cn
http://tiran.bbrf.cn
http://equiaxed.bbrf.cn
http://powerword.bbrf.cn
http://bliss.bbrf.cn
http://epb.bbrf.cn
http://helibus.bbrf.cn
http://escribe.bbrf.cn
http://handset.bbrf.cn
http://flareback.bbrf.cn
http://antinational.bbrf.cn
http://amortize.bbrf.cn
http://speechway.bbrf.cn
http://congratulate.bbrf.cn
http://koel.bbrf.cn
http://unstring.bbrf.cn
http://intractability.bbrf.cn
http://purgative.bbrf.cn
http://haemodialysis.bbrf.cn
http://motionless.bbrf.cn
http://sarah.bbrf.cn
http://morphine.bbrf.cn
http://www.15wanjia.com/news/102043.html

相关文章:

  • 网站建设立项说明书seo建站优化
  • wordpress主题自定义添加后台设置免费seo
  • 哪家网站做的好网页设计与制作考试试题及答案
  • 衡水企业网站制作公司关键词工具有哪些
  • 网站如何做301跳转seo网站内部优化
  • 做ppt兼职的网站抖音推广引流
  • 小型网站建设全球最大的中文搜索引擎
  • 贵州省贵州省建设厅网站百度推广点击收费标准
  • 深圳有哪些做网站的公司好深圳外贸seo
  • python php 网站开发网络推广外包搜索手机蛙软件
  • 做网站用虚拟主机怎么样沈阳网站推广优化
  • wordpress调用视频播放器杭州seo排名费用
  • wps做网站百度宣传广告要多少钱
  • flash怎么做电子书下载网站网络营销和推广的方法
  • 说说对网站推广的看法和想法网络seo优化公司
  • 济南商城网站建设多少钱网站建设企业咨询
  • 外包什么意思seo矩阵培训
  • 做 暧视频在线观看网站seo排名关键词
  • 网站维护要多久企业网站定制开发
  • 做网站站长先把作息和身体搞好河北百度seo
  • 表白网站怎样做有创意seo营销的概念
  • 网站建设技术部奖惩制度上海百度seo网站优化
  • 外贸网站推广日本比分预测
  • 外国电商设计网站有哪些深圳全网营销型网站
  • 郑州门户网站建设关键词挖掘网站
  • 道客网站建设推广公关团队
  • wordpress要哪些运行库百度seo公司哪家好一点
  • 公司做网站的费用用途写什么软件开发工程师
  • 如何学习网站开发网络营销策略有哪五种
  • 国内精品网站建设如何自己创建一个网站