使用if语句进行判断

public class TestDemo {
public static void main(String args[]) {
double score = 90.0; // 定义变量
if (score > 60.0) { // 设置判断条件
System.out.println("及格了!");
}
}
} 程序执行结果: 及格了!
使用if…else判断
public class TestDemo {
public static void main(String args[]) {
double score = 30.0; // 定义变量
if (score > 60.0) { // 条件判断满足
System.out.println("及格了!");
} else { // 条件判断不满足
System.out.println("小白的成绩!");
}
}
}
程序执行结果: 小白的成绩!
使用if…else if…else判断
public class TestDemo {
public static void main(String args[]) {
double score = 91.0; // 定义变量
if (score < 60.0) { // 条件判断
System.out.println("小白的成绩!") ;
} else if (score >= 60 && score <= 90) {// 条件判断
System.out.println("中等成绩") ;
} else if (score > 90 && score <= 100) {// 条件判断
System.out.println("优秀成绩") ;
} else { // 条件判断都不满足
System.out.println("你家的考试成绩这么怪异!") ;
}
}
} 程序执行结果: 优秀成绩
switch

对于多条件判断使用if..else if…else是可以判断布尔条件的,如果是多数值判断,可以通过switch完成,语法如下

switch(整数 | 字符 | 枚举 | String) {
case 内容 : {
内容满足时执行 ;
break ;
}
case 内容 : {
内容满足时执行 ;
break ;
}
case 内容 : {
内容满足时执行 ;
break ;
} ...
default : {
内容都不满足时执行 ;
break ;
}
}
public class TestDemo {
public static void main(String args[]) {
int ch = 1;
switch (ch) { // 判断的是数字
case 2: { // 判断内容是否是2
System.out.println("内容是2");
break;
}
case 1: { // 判断内容是否是1
System.out.println("内容是1");
break;
}
case 3: { // 判断内容是否是3
System.out.println("内容是3");
break;
}
default: { // 判断都不满足
System.out.println("没有匹配内容");
break;
}
}
}
} 程序执行结果: 内容是1
public class TestDemo {
public static void main(String args[]) {
String str = "HELLO";
switch (str) { // 判断的是字符串
case "HELLO": {
System.out.println("内容是HELLO");
break;
}
case "hello": {
System.out.println("内容是hello");
break;
}
case "mldn": {
System.out.println("内容是mldn");
break;
}
default: {
System.out.println("没有匹配内容");
break;
}
}
}
}
程序执行结果: 内容是HELLO
实现1 ~ 100的累加 —— 使用while循环
public class TestDemo {
public static void main(String args[]) {
int sum = 0; // 保存总和
int current = 1; // 循环的初始化条件
while (current <= 100) { // 循环结束条件
sum += current; // 累加
current++; // 改变循环条件
}
System.out.println(sum);
}
} 程序执行结果: 5050
使用do..while循环实现累加操作
public class TestDemo {
public static void main(String args[]) {
int sum = 0; // 保存总和
int current = 1; // 循环的初始化条件
do { // 循环结束条件
sum += current; // 累加
current++; // 改变循环条件
} while (current <= 100); // 循环结束判断
System.out.println(sum);
}
} 程序执行结果: 5050
使用for循环实现1 ~ 100累加
public class TestDemo {
public static void main(String args[]) {
int sum = 0; // 保存总和
// 设置循环初始化条件current,同时此变量作为累加操作使用
// 每次执行循环体前都要进行循环判断(current <= 100)
// 循环体执行完毕后会自动执行“current++”改变循环条件
for (int current = 1; current <= 100; current++) {
sum += current; // 循环体中实现累加操作
}
System.out.println(sum);
}
} 程序执行结果: 5050
输出乘法口诀表
public class TestDemo {
public static void main(String args[]) {
for (int x = 1; x <= 9; x++) { // 控制循环的行数
for (int y = 1; y <= x; y++) {// 控制列数
System.out.print(x + "*" + y + "=" + (x * y) + "\t");
}
System.out.println();// 换行
}
}
} 1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
循环控制

正常情况下只要执行了循环,那么只要循环条件满足,循环体的代码就会一直执行,但是在程序之中也提供有两个循环停止的控制语句:continue(退出本次循环)、break(退出整个循环)。此类的语句在使用时往往要结合分支语句进行判断。

观察continue
public class TestDemo {
public static void main(String args[]) {
for (int x = 0; x < 10; x++) {
if (x == 3) {
continue; // 之后的代码不执行,直接结束本次循环
}
System.out.print("x = " + x + "、");
}
}
}
程序执行结果: x = 0、x = 1、x = 2、x = 4、x = 5、x = 6、x = 7、x = 8、x = 9、
观察break
public class TestDemo {
public static void main(String args[]) {
for (int x = 0; x < 10; x++) {
if (x == 3) {
break; // 退出整个循环
}
System.out.print("x = " + x + "、");
}
}
}
程序执行结果: x = 0、x = 1、x = 2、

最新文章

  1. cent6.4使用
  2. 移动端meta
  3. javascript语言精粹
  4. python学习笔记-(八)装饰器、生成器&amp;迭代器
  5. mac liteIDE调试配置
  6. 在 Excel 中使用正则表达式进行查找与替换
  7. android 6.0 httpclient
  8. 关于页面滚动值scrollTop在FireFox与Chrome浏览器间的兼容问题
  9. HDU 4460
  10. GSM嗅探
  11. Oracle 日期类型timestamp(时间戳)和date类型使用
  12. HDOJ(HDU) 1708 Fibonacci String
  13. 20.org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
  14. JavaFX基础学习之URLConnection
  15. PHP初入,简易网页整理(布局&amp;特效的使用)
  16. IIS发布 用户 \&#39;IIS APPPOOL\\X\&#39; 登录失败
  17. 基于嵌入式操作系统VxWorks的多任务并发程序设计(1)――基本概念
  18. delphi中响应鼠标进入或离开控件的方法
  19. [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal
  20. verilog 之流水灯

热门文章

  1. 用tkinter制作签名设计窗口
  2. OmniPlan,一款让你无法自拔的项目管理工具(仅适用于MAC系统)
  3. RedisDesktopManager如何使用命令行?
  4. JS实现选择排序
  5. Unity 着色器
  6. POJ 2449 Remmarguts&#39; Date (第k短路径)
  7. WPF界面+halcon生成的C#文件
  8. hystrix实战总结;
  9. bzoj1009 KMP+矩阵dp
  10. ansible-playbook 单个yml文件部署tomcat简单示例