5、程序控制结构
程序控制结构
1、介绍
2、顺序控制
3、分支控制 if-else
3.1、单分支
import java.util.Scanner;
public class If01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年龄:");
//把年龄保存到一个变量
int age = sc.nextInt();
if (age > 18) {
System.out.println("你年龄大于18,要对自己的行为负责,送入监狱");
} else {
System.out.println("你还是未成年,好好读书");
}
}
}
3.2、双分支
import java.util.Scanner;
public class If02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年龄:");
//把年龄保存到一个变量
int age = sc.nextInt();
if (age > 18) {
System.out.println("你年龄大于18,要对自己的行为负责,送入监狱");
} else {
System.out.println("你的年龄不大这次放过你了");
}
}
}
3.3、单分支和双分支练习题
**2: **
import java.util.Scanner;
public class IfExercise01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
double d1 = sc.nextDouble();
System.out.println("请输入第二个数:");
double d2 = sc.nextDouble();
if (d1 > 10.0 && d2 < 20.0) {
System.out.println(d1 + d2);
} else {
System.out.println("程序继续...");
}
}
}
**3: **
import java.util.Scanner;
public class IfExercise02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
int n1 = sc.nextInt();
System.out.println("请输入第二个数:");
int n2 = sc.nextInt();
int sum = n1 + n2;
if ( sum % 3 == 0 && sum % 5 == 0) {
System.out.println(sum + "能被3和5整除");
} else {
System.out.println(sum + "不能被3和5整除");
}
}
}
**4: **
import java.util.Scanner;
public class IfExercise03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个年份:");
int year = sc.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println(year + "是闰年");
} else {
System.out.println(year + "不是闰年");
}
}
}
3.4、多分支
import java.util.Scanner;
public class If03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入芝麻信用分(1-100):");
int score = sc.nextInt();
//对输入的信用分,进行一个范围的有效判断
if ( score <= 100 && score > 0 ) {
if (score == 100) {
System.out.println("信用极好");
} else if (score > 80 && score <= 99) {
System.out.println("信用优秀");
} else if (score >= 60 && score <= 80) {
System.out.println("信用一般");
} else {
System.out.println("信用不及格");
}
} else {
System.out.println("信用分需要在1-100,请重新输入");
}
}
}
答案是:b
4、嵌套分支
import java.util.Scanner;
public class NestedIf {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入初赛成绩:");
double score = sc.nextDouble();
if (score > 8.0) {
System.out.println("恭喜你进入决赛");
System.out.println("请输入性别:");
//取第一个字符
char gender = sc.next().charAt(0);
if (gender == '男') {
System.out.println("进入男生组");
} else if (gender == '女') {
System.out.println("进入女生组");
} else {
System.out.println("性别输入错误,请重新输入");
}
} else {
System.out.println("不好意思,你被淘汰了");
}
}
}
import java.util.Scanner;
public class NestedIf02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
System.out.println("请输入出游的月份:");
int month = sc.nextInt();
//定义旺季成人的票价
int price = 60;
if (age > 0 && age < 18) {
if (month >= 4 && month <= 10) {
System.out.println("儿童票价为" + (price * (1.0 / 2)));
} else {
System.out.println("儿童票价为" + 20);
}
} else if (age >= 18 && age <= 60) {
if (month >= 4 && month <= 10) {
System.out.println("成人票价为" + price);
} else {
System.out.println("成人票价为" + 40);
}
} else if (age > 60) {
if (month >= 4 && month <= 10) {
System.out.println("老人票价为" + (price * (1.0 / 3)));
} else {
System.out.println("老人票价为" + 20);
}
} else {
System.out.println("年龄输入错误,请重新输入");
}
}
}
5、switch分支结构
5.1、基本语法
5.2、流程图
5.3、快速入门
import java.util.Scanner;
public class Switch01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符(a-g):");
char c = sc.next().charAt(0);
switch(c) {
case 'a':
System.out.println("星期一");
break;
case 'b':
System.out.println("星期二");
break;
case 'c':
System.out.println("星期三");
break;
case 'd':
System.out.println("星期四");
break;
case 'e':
System.out.println("星期五");
break;
case 'f':
System.out.println("星期六");
break;
case 'g':
System.out.println("星期日");
break;
default:
System.out.println("输入错误,请重新输入");
}
}
}
5.4、注意事项与细节
5.5、课堂练习
**1: **
import java.util.Scanner;public class SwitchExercise01 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符(a-e):"); char c = sc.next().charAt(0); switch (c) { case 'a': System.out.println("A"); break; case 'b': System.out.println("B"); break; case 'c': System.out.println("C"); break; case 'd': System.out.println("D"); break; case 'e': System.out.println("E"); break; default: System.out.println("other"); } }}
**2: **
import java.util.Scanner;public class SwitchExercise02 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入学生成绩(0-100):"); double score = sc.nextDouble(); //如果成绩在 [60,100],(int)(成绩/60) = 1 //如果成绩在 [0,60),(int)(成绩/60) = 0 if (score >= 0 && score <= 100) { switch ((int)(score / 60)) { case 0: System.out.println("不合格"); break; case 1: System.out.println("合格"); break; } } else { System.out.println("输入错误,请重新输入"); } }}
**3: **
import java.util.Scanner;public class SwitchExercise03 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入月份(1-12):"); int month = sc.nextInt(); switch (month) { case 3: case 4: case 5: System.out.println("春季"); break; case 6: case 7: case 8: System.out.println("夏季"); break; case 9: case 10: case 11: System.out.println("秋季"); break; case 12: case 1: case 2: System.out.println("冬季"); break; default: System.out.println("输入错误,请重新输入"); } }}
5.6、switch和if的比较
6、for循环控制
6.1、基本介绍
6.2、基本语法
6.3、执行流程分析
6.4、注意事项和细节说明
public class ForDetail {
public static void main(String[] args) {
int count = 3;
/* 执行流程
i=0,j=0 0 < 3? T => i=0 j=0
i++,j+=2 i=1,j=2 1 < 3? T => i=1 j=2
i++,j+=2 i=2,j=4 2 < 3? T => i=2 j=4
i++,j+=2 i=3,j=6 3 < 3? F => 跳出循环,结束
输出结果:
i=0 j=0
i=1 j=2
i=2 j=4
*/
for (int i = 0,j = 0; i < count; i++,j+=2) {
System.out.println("i = " + i + " j = " + j);
}
}
}
6.5、练习题
**1: **
public class ForExercise01 { public static void main(String[] args) { //打印1~100之间所有是9的倍数的整数,统计个数及总和 //定义个数 int count = 0; //定义总和 int sum = 0; //先死后活编程思想 //1.为了适应更好的需求,把范围的开始的值和结束的值,做出变量 //2.9倍数也做成变量 int start = 1; //开始的值 int end = 100; //结束的值 int multiple = 9; //倍数 for (int i = start; i <= end; i++) { if (i % multiple == 0) { System.out.print(i + " "); count++; sum+=i; } } //换行 System.out.println(""); System.out.println("符合条件的一共有" + count + "个,总和为" + sum); }}
**2: **
public class ForExercise02 { public static void main(String[] args) { /* 执行流程 i=0,j=5 0 <= 5? T => 0 + 5 = 5 i++,j-- i=1,j=4 1 <= 5? T => 1 + 4 = 5 i++,j-- i=2,j=3 2 <= 5? T => 2 + 3 = 5 i++,j-- i=3,j=2 3 <= 5? T => 3 + 2 = 5 i++,j-- i=4,j=1 4 <= 5? T => 4 + 1 = 5 i++,j-- i=5,j=0 5 <= 5? T => 5 + 0 = 5 i++,j-- i=6,j=-1 6 <= 5? F => 跳出循环 */ int start = 0; int end = 5; for (int i = start,j = end; i <= end; i++,j--) { /*System.out.print(i + " + "); System.out.print(j); System.out.println(" = " + (i+j));*/ System.out.println(i + " + " + j + " = " + (i+j)); } }}
7、while循环控制
7.1、基本语法
7.2、执行流程分析
public class While01 { public static void main(String[] args) { //提高代码的灵活性 int start = 1; int end = 10; while (start <= end) { System.out.println("Hello, Java " + start); start++; //循环变量迭代 } }}
7.3、注意事项和细节说明
7.4、练习
**1: **
public class WhileExercise01 {
public static void main(String[] args) {
int start = 1;
int end = 100;
while (start <= end) {
if (start % 3 == 0) {
System.out.print(start + " ");
}
start++;
}
}
}
**2: **
public class WhileExercise02 {
public static void main(String[] args) {
int start = 40;
int end = 200;
while (start <= end) {
if (start % 2 == 0) {
System.out.print(start + " ");
}
start++;
}
}
}
8、do..while循环控制
8.1、基本语法
8.2、执行流程分析
public class DoWhile01 { public static void main(String[] args) { int start = 1; int end = 10; do { System.out.println("Hello, Java " + start); start++; } while (start <= end); }}
8.3、注意事项和细节说明
**1: **
public class DoWhileExercise01 { public static void main(String[] args) { //打印1-100 int start = 1; int end = 100; do { System.out.print(start + " "); start++; //十个换一行 if ( start % 10 == 0) { System.out.println(""); } } while (start <= end); }}
**2: **
public class DoWhileExercise02 { public static void main(String[] args) { int start = 1; int end = 100; int sum = 0; do { sum+=start; start++; } while (start <= end); System.out.println("和为:" + sum); }}
**3: **
public class DoWhileExercise03 { public static void main(String[] args) { int start = 1; int end = 200; int firstDivisor = 5; //第一个除数 int secondDivisor = 3; //第二个除数 int count = 0; do { if (start % firstDivisor == 0 && start % secondDivisor != 0) { count++; } start++; } while (start <= end); System.out.println("个数为:" + count); //27 }}
**4: **
import java.util.Scanner;public class DoWhileExercise04 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); char yes = 'y'; char no = 'n'; char answer = ' '; //初始化 do { System.out.println("还钱吗?(y/n):"); answer = sc.next().charAt(0); System.out.println("使出五连鞭"); } while (answer == no); }}
9、多重循环控制
9.1、介绍
9.2、执行步骤分析
public class Multiple { public static void main(String[] args) { /* 双层for i=0 0<2? T => j=0 0<3? T => i=0 j=0 j++ j=1 1<3? T => i=0 j=1 j++ j=2 2<3? T => i=0 j=2 j++ j=3 3<3? F => 跳出内循环 i++ i=1 1<2? T => j=0 0<3? T => i=1 j=0 j++ j=1 1<3? T => i=1 j=1 j++ j=2 2<3? T => i=1 j=2 j++ j=3 3<3? F => 跳出内循环 i++ i=2 2<2? F => 跳出外循环 输出结果: i=0 j=0 i=0 j=1 i=0 j=2 i=1 j=0 i=1 j=1 i=1 j=2 */ for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { System.out.println("i = " + i + " j = " + j); } } }}
9.3、应用实例
1、2:
import java.util.Scanner;public class MulForExercise01 { public static void main(String[] args) { //统计3个班成绩情况,每个班有5名同学,求出各个班的平均分和所有班级的平均分 /* 思路分析: 1.先计算一个班,5个学生的成绩,使用for 创建 scanner 对象,接收用户输入 */ Scanner sc = new Scanner(System.in); //定义三个班的总分 double totalScore = 0; //三个班的及格人数 int totalPass = 0; for (int i = 1; i <= 3; i++) { //定义班级的总分 double sum = 0; for (int j = 1; j <= 5; j++) { System.out.println("请输入" + i + "班第" + j + "个学生的成绩"); double score = sc.nextDouble(); //判断是否及格 if (score >= 60) { totalPass++; } sum+=score; } double average = sum / 5; System.out.println(i + "班总分为:" + sum + ", " + i + "班的平均分为:" + average); totalScore+=sum; } System.out.println("所有班级总分为:" + totalScore + ", 所有班的平均分为:" + (totalScore / 15)); System.out.println("所有班级的及格人数为:" + totalPass); }}
**3: **
import java.util.Scanner;public class MulForExercise02 { public static void main(String[] args) { for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + " * " + i + " = " + (i * j) + "\t"); } //换行 System.out.println(""); } }}
9.4、经典的打印金字塔
**1: **
import java.util.Scanner;public class Stars01 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入层数(1~*):"); int totalLevel = sc.nextInt(); for (int i = 1; i <= totalLevel; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } //换行 System.out.println(""); } }}
**2: **
import java.util.Scanner;public class Stars04 { public static void main(String[] args) { /* 打印整个金字塔 * //第1层 有1个* 2*1-1 有5=(总层数-1)个空格 *** //第2层 有3个* 2*2-1 有4=(总层数-2)个空格 ***** //第3层 有5个* 2*3-1 有3=(总层数-3)个空格 ******* //第4层 有7个* 2*4-1 有2=(总层数-4)个空格 ********* //第5层 有9个* 2*5-1 有1=(总层数-5)个空格 *********** //第6层 有11个* 2*6-1 有0=(总层数-6)个空格 ... //第...层 有2*n-1 有(总层数 - 当前层数)个空格 */ Scanner sc = new Scanner(System.in); System.out.println("请输入层数(1~*):"); int totalLevel = sc.nextInt(); for (int i = 1; i <= totalLevel; i++) { //输出空格 for (int j = 1; j <= totalLevel - i; j++) { System.out.print(" "); } //输出* for (int k = 1; k <= (2 * i - 1); k++) { System.out.print("*"); } //换行 System.out.println(""); } }}
**3: **
import java.util.Scanner;public class Stars03 { public static void main(String[] args) { /* 打印空心金字塔 * //第1层 有1个* 有3=(总层数-1)个空格 输出第1个*和第1=(2 * 1 - 1)个* * * //第2层 有2个* 有2=(总层数-2)个空格 输出第1个*和第3=(2 * 2 - 1)个* * * //第3层 有2个* 有1=(总层数-3)个空格 输出第1个*和第5=(3 * 2 - 1)个* ******* //第4层 有7个* 有0=(总层数-4)个空格 最后一层全部输出 第一层和最后一层(当前层数*2-1) (总层数-当前层数)个空格 输出第1个*和(当前层数 * 2 -1)个* */ Scanner sc = new Scanner(System.in); System.out.println("请输入层数(1~*):"); int totalLevel = sc.nextInt(); for (int i = 1; i <= totalLevel; i++) { //输出空格 for (int j = 1; j <= totalLevel -i; j++) { System.out.print(" "); } //输出* for (int k = 1; k<= (2 * i - 1); k++) { if (k == 1 || k == (2 * i - 1) || i== totalLevel) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(""); } }}
**4: **
import java.util.Scanner;public class Stars05 { public static void main(String[] args) { /* 打印菱形 分成上下两部分 上有totalLevel层 下有(totalLevel - 1)层 * //第1层 有1个* 有2个空格 *** //第2层 有3个* 有1个空格 ***** //第3层 有5个* (2*当前层数-1) 有0个空格 ((总层数 + 1) / 2 - 当前层数) 中间层 (总层数 + 1) / 2 *** //第1层 有3个* 有1个空格 * //第2层 有1个* 有2个空格 */ Scanner sc = new Scanner(System.in); System.out.println("请输入层数(1~*):"); int totalLevel = sc.nextInt(); //上半部分 // for (int i = 1; i <= totalLevel; i++) { //层数 for (int k = 1; k <= totalLevel - i; k++) { System.out.print(" "); } for (int j = 1; j <= (2 * i - 1); j++) { System.out.print("*"); } System.out.println(""); } for (int i = 1; i <= totalLevel -1; i++) { //层数 for (int k = 1; k <= i; k++) { System.out.print(" "); } //从下往上打印* 满足 2 * 当前层数 - 1 for (int j = 2 * totalLevel - 1; j >= 2 * i - 1 + 2; j--) { System.out.print("*"); } System.out.println(" "); } }}
**5: **
import java.util.Scanner;public class Stars06 { public static void main(String[] args) { /* 打印空心菱形 分成上下两部分 上有totalLevel层 下有(totalLevel - 1)层 * //第1层 有1个* 有2个空格 打印第一个和最后一个* * * //第2层 有2个* 有1个空格 * * //第3层 有2个* 有0个空格 * * //第1层 有2个* 有1个空格 * //第2层 有1个* 有2个空格 */ Scanner sc = new Scanner(System.in); System.out.println("请输入层数(1~*):"); int totalLevel = sc.nextInt(); //上半部分 for (int i = 1; i <= totalLevel; i++) { //层数 for (int k = 1; k <= totalLevel - i; k++) { System.out.print(" "); } for (int j = 1; j <= (2 * i - 1); j++) { if (j == 1 || j == (2 * i - 1)) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(""); } for (int i = 1; i <= totalLevel -1; i++) { //层数 for (int k = 1; k <= i; k++) { System.out.print(" "); } //从下往上打印* 满足 2 * 当前层数 - 1 for (int j = 2 * totalLevel - 1; j >= 2 * i - 1 + 2; j--) { if (j == 2 * totalLevel - 1 || j == 2 * i - 1 + 2) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(" "); } }}
10、跳转控制语句-break
10.1、需求
import java.lang.Math;public class Break01 { public static void main(String[] args) { int count = 0; //死循环 while (true) { int num = (int)(Math.random() * 100) + 1; if (num != 97) { count++; } else { break; } } System.out.println("生成97用了" + count + "次"); }}
10.2、基本介绍
10.3、快速入门
public class Break02 { public static void main(String[] args) { int start = 1; int end = 10; int condition = 3; //跳出循环的条件 int count = 0; //记录循环了多少次 for (int i = start; i <= end; i++) { if (i == condition) { break; } count++; System.out.println("ok" + i); } System.out.println("循环了" + count + "次"); }}
10.4、注意事项和细节说明
public class BreakDetail { public static void main(String[] args) { /* 执行流程分析 j=0 j<4 ? T => i=0 i<10? T => i==2? F => i=0 i++ i=1 1<10? T => 1==2? F => i=1 i++ i=2 2<10? T => 2==2? T => break label1 跳出循环 预计输出结果: i=0 i=1 */ label1: for (int j = 0; j < 4; j++) { label2: for (int i = 0; i < 10; i++) { if (i == 2) { break label1; } System.out.println("i = " + i); } } }}
10.5、练习
**1: **
public class BreakExercise01 { public static void main(String[] args) { int sum = 0; int n = 0; for (int i = 1; i <= 100; i++) { sum+=i; if (sum > 20) { n = i; break; } } System.out.println("sum =" + sum); System.out.println("当前数为:" + n); }}
2:
import java.util.Scanner;public class BreakExercise02 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int opportunity = 3; //机会 for (int i = opportunity; i >= 1; i--) { System.out.println("请输入用户名:"); String name = sc.next(); System.out.println("请输入密码:"); String pwd = sc.next(); if ("丁真".equals(name) && "666".equals(pwd)) { System.out.println("登录成功"); break; } else { System.out.println("账号或者密码错误,你还有" + (i - 1) + "次机会"); } } }}
11、跳转控制语句-continue
11.1、基本介绍
11.2、快速入门
public class Continue01 { public static void main(String[] args) { /* 执行流程分析 i=1 i<=4? T => i++ i=2 2==2? T => continue 2<=4? T => i++ i=3 3==2? F => i=3 3<=4? T => i++ i=4 4==2? F => i=4 4<=4? T => i++ i=5 5==2? F => i=5 5<=4? F => 退出循环 输出结果: i=3 i=4 i=5 */ int i = 1; while (i <= 4) { i++; if (i == 2) { continue; } System.out.println("i = " + i); } }}
11.3、细节分析和说明
public class ContinueDetail { public static void main(String[] args) { /* 执行流程分析 j=0 j<4? T => i=0 i<10? T => 0==2? F => i=0 i++ i=1 1<10? T => 1==2? F => i=1 i++ i=2 2<10? T => 2==2? T => continue i++ i=3 3<10? T -> 3==2? F => i=3 ... j++ j=1 1<4? T =>... ... 输出结果: i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 ... 当 continue label1时 输出结果: i=0 i=1 i=0 i=1 i=0 i=1 i=0 i=1 */ label1: for (int j = 0; j < 4; j++) { lable2: for (int i = 0; i < 10; i++) { if (i == 2) { //continue; // 等价于 lable2 //continue label1; continue lable2; } System.out.println("i = " + i); } } }}
当 continue label1时:
当 continue label 或者 continue labe2 时:
12、跳转控制语句-return
12.1、介绍
public class Return01 { public static void main(String[] args) { /* 执行流程分析 i=1 1<=5? T => 1==3? F => Hello Java! i++ i=2 2<=5? T => 2==3? F => Hello Java! i++ i=3 3<=5? T => 3==3? T => Java 天下第一3 => return 跳出方法 输出结果: Hello Java! Hello Java! Java 天下第一3 为 contine 时: Hello Java! Hello Java! Java 天下第一3 Hello Java! Hello Java! go on... 为 break 时: Hello Java! Hello Java! Java 天下第一3 go on... */ for (int i = 1; i <= 5; i++) { if (i == 3) { System.out.println("Java 天下第一" + i); return; //continue; //break; } System.out.println("Hello Java!"); } System.out.println("go on..."); }}
为 return 时
为 continue 时
为 break 时
13、作业
**1: **
public class Homework01 { public static void main(String[] args) { double money = 100000; //定义次数 int count = 0; while (true) { if (money > 50000) { money = money - (money * 0.05); count++; } else if (money >= 1000) { money = money - 1000; count++; } else if (money < 1000) { break; } else { System.out.println("输入错误,请重新输入"); } } System.out.println("剩余" + money + "元, 不足以通行"); System.out.println("可以经过" + count + "个路口"); }}
**2: **
import java.util.Scanner;public class Homework02 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数:"); int num = sc.nextInt(); if (num > 0) { System.out.println(num + "大于0"); } else if (num == 0) { System.out.println(num + "等于0"); } else { System.out.println(num + "小于0"); } }}
**3: **
import java.util.Scanner;public class Homework03 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个年份:"); int year = sc.nextInt(); if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { System.out.println(year + "是闰年"); } else { System.out.println(year + "不是闰年"); } }}
**4: **
import java.util.Scanner;public class Homework04 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个三位数:"); int num = sc.nextInt(); //个位 int ge = num % 10; //十位 int shi = num % 100 / 10; //百位 int bai = num / 100; if (num == ((ge * ge * ge) + (shi * shi * shi) + (bai * bai * bai))) { System.out.println(num + "是水仙花数"); } else { System.out.println(num + "不是水仙花数"); } }}
public class Homework05 { public static void main(String[] args) { /* 执行流程分析 m=0 n=3 0>0? F => */ int m = 0; int n = 3; if (m > 0) { if (n > 2) { System.out.println("ok1"); } else { System.out.println("ok2"); } } }}
没有任何输出
**6: **
public class Homework06 { public static void main(String[] args) { //定义个数 int count = 0; for (int i = 1; i <= 100; i++) { if (i % 5 != 0) { count++; System.out.print(i + "\t"); } //每满5个就换行 if (count % 5 == 0) { System.out.println(""); } } }}
**7: **
public class Homework07 { public static void main(String[] args) { char c1 = 'a'; char c2 = 'z'; char c3 = 'Z'; char c4 = 'A'; for (int i = c1; i <= c2; i++) { System.out.print((char)i + " "); } System.out.println(""); for (int i = c3; i >= c4; i--) { System.out.print((char)i + " "); } }}
**8: **
public class Homework08 { public static void main(String[] args) { double sum = 0; for (int i = 1; i <= 100; i++) { //如果是奇数 if (i % 2 != 0) { sum+=(1.0/i); } else { sum-=(1.0/i); } } System.out.println("和为:" + sum); }}
**9: **
public class Homework09 { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 100; i++) { for (int j = 1; j <= i; j++) { sum+=j; } } System.out.println("和为:" + sum); }}
14、两个编程思想
- 化繁为简:将复杂的需求,拆解成简单的需求,逐步完成
- 先死后活:先考虑固定的值,然后转成可以灵活变化的值