Topic05(控制流程)
2019-02-09 本文已影响0人
KaveeDJ
05.01 if
- 计算是不是闰年
package pack;
import java.util.Scanner;
public class IsLeapYear {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入年份");
int year = s.nextInt();
if ((0 == year % 4 && 0 != year % 100) || 0 == year % 400) {
System.out.println(year + "是闰年");
} else {
System.out.println(year + "不是闰年");
}
}
}
05.02 switch
- 在每个表达式结束,都应该有一个break
- String在Java1.7之前是不支持的,Java从1.7开始支持switch用String的,编译后是把String转化为hash值,其实还是整数
- 练习,用switch判断季节
05.03 while
- do while至少会执行一次
- 练习:求阶乘
package pack;
import java.util.Scanner;
public class factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数");
int n = sc.nextInt();
int fac = 1;
while (n >= 1) {
fac *= n;
n--;
}
System.out.println("阶乘是:" + fac);
}
}
05.04 for
- for是最常用的
05.05 continue
- 结束本次循环,进入下一循环
- 练习,打印1-100之间的数,如果这个数,要么是3,要么5的倍数,就忽略掉
package pack;
public class PrintSpecialNumber {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (0 == i%3 || 0 == i%5){
continue;
}
System.out.println(i);
}
}
}
05.06 break
- 结束当前循环
- 练习,复利20%,多少年成为百万富翁
package pack;
public class CompoundInterest {
public static void main(String[] args) {
int fundPerMonth = 1000;
int fundPerYear = fundPerMonth * 12;
float rate = 0.20f;
int sum = 0;
int target = 1000*1000;
for (int j = 1; j < 100; j++) {
int year = j;
float compoundInterestRate = 1;
for (int i = 0; i < year; i++) {
compoundInterestRate = compoundInterestRate * (1 + rate);
}
int compoundInterest = (int)(fundPerYear * compoundInterestRate);
sum += compoundInterest;
System.out.println("经过" + year + " 年,总收入 " + sum);
if(sum >= target) {
System.out.println("一共需要" + year + "年,累计收入超过" + target);
break;
}
}
}
}
05.07 结束外部循环
- 借助boolean变量结束外部循环,需要在内部循环中,修改这个变量值
- 也可以在外部循环加一个标签
- 练习:黄金分割(Golden Cut)
package pack;
public class GoldenCut {
public static void main(String[] args) {
// 寻找某两个数相除,其结果离黄金分割点0.618最近
// 分母和分子不能同时为偶数
// 分子和分母的取值范围[1,20]
int range = 20;
float breakPoint = 0.618f;
float minDiff = 100;
int answerFenzi = 0;
int answerFenmu = 0;
for (int fenzi = 1; fenzi <= range; fenzi++) {
for (int fenmu = 1; fenmu <= range; fenmu++) {
// 分母和分子不能同时为偶数
if (0 == fenzi % 2 & 0 == fenmu % 2) {
continue;
}
// 取值
float value = (float) fenzi / fenmu;
// 取离黄金分割点的差值
float diff = value - breakPoint;
// 绝对值
diff = diff < 0 ? 0 - diff : diff;
// 找出最小的差值
if (diff < minDiff) {
minDiff = diff;
answerFenzi = fenzi;
answerFenmu = fenmu;
}
}
System.out.println("离黄金分割点(" + breakPoint + ")最近的两个数相除是:" + answerFenzi + "/" + answerFenmu + "=" + ((float)answerFenzi / answerFenmu));
}
}
}
- 练习:找到水仙花数
package pack;
public class NarcissisticNumber {
public static void main(String[] args) {
for (int i = 0; i < 10000; i++) {
int baiwei = i / 100;
int shiwei = i /10 % 10;
int gewei = i % 10;
int cube = baiwei * baiwei * baiwei + shiwei * shiwei * shiwei + gewei * gewei * gewei;
if (cube == i) {
System.out.println("找到水仙花数:" + i);
}
}
}
}
- 小练习
package pack;
public class Puzzle {
public static void main(String[] args) {
int a = 0;
int b = 0;
int c = 0;
int d = 0;
for (a = -100; a <= 100; a++) {
for (b = -100; b <= 100; b++) {
for (c = -100; c <= 100; c++) {
for (d = -100; d <= 100; d++) {
if (a+b==8 && c-d==6 && a+c == 14 && b+d == 10) {
System.out.println("a:"+a);
System.out.println("b:"+b);
System.out.println("c:"+c);
System.out.println("d:"+d);
}
}
}
}
}
}
}