JAVA基础教程
学习JAVA网站 : how2j
第五讲 控制流程
控制流程 if
- if 关注一下可能遇到的问题叭
在第6行,if后面有一个分号; 而分号也是一个完整的表达式。 如果b为true,会执行这个分号,然后打印yes;如果b为false,不会执行这个分号,然后打印yes。这样,看上去无论如何都会打印yes。
public class HelloWorld {
public static void main(String[] args) {
boolean b = false;
if (b);
System.out.println("yes");
}
}
控制流程 switch
switch可以使用byte,short,int,char,String,enum
注: 每个表达式结束,都应该有一个break; 注: String在Java1.7之前是不支持的, Java从1.7开始支持switch用String的,编译后是把String转化为hash值,其实还是整数 注: enum是枚举类型,在枚举章节有详细讲解
通过Scanner 输入月份,然后使用switch 判断季节
System.out.print("请输入月份来进行判断该月是什么季节");
int month=scanner.nextInt();
switch (month) {
case 3,4,5:
System.out.print(month+"是春季");
break;
case 6,7,8:
System.out.print(month+"是夏季");
break;
case 9,10,11:
System.out.print(month+"是秋季");
break;
case 12,1,2:
System.out.print(month+"是冬季");
break;
default:
System.out.print(month+"不是正确的月份");
}
}
控制流程 while
通过Scanner 获取一个整数,然后使用while计算这个整数的阶乘
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in)
int input = sc.nextInt();
int loop = 1;
int res = 1;
while(loop <= input){
res = res * loop;
loop++;
}
System.out.println(res);
}
}
控制流程 for
天朝有一个乞丐姓洪,去天桥要钱 第一天要了1块钱 第二天要了2块钱 第三天要了4块钱 第四天要了8块钱 以此类推
问题: 洪乞丐干10天,收入是多少?
public class Main {
public static void main(String[] args) {
int sum = 0;
int a = 1,b = 2;
for (int i=1;i <= 10;i++) {
sum += a;
a = a * b;
}
System.out.println(sum);
}
}
控制流程 continue
如果是双数,后面的代码不执行,直接进行下一次循环
public class HelloWorld {
public static void main(String[] args) {
//打印单数
for (int j = 0; j < 10; j++) {
if(0==j%2)
continue; //如果是双数,后面的代码不执行,直接进行下一次循环
System.out.println(j);
}
}
}
示例 2 : 练习-忽略倍数
- 练习-忽略倍数
打印 1-100 之间的数,如果这个数,要么是3,要么5的倍数,就忽略掉。
public class Main {
public static void main(String[] args) {
for (int i = 1;i <= 100;i++) {
if(i % 3 == 0)
continue;
if (i % 5 == 0)
continue;
System.out.println(i);
}
}
}
控制流程 break
- break 直接结束当前for循环
public class HelloWorld {
public static void main(String[] args) {
//打印单数
for (int j = 0; j < 10; j++) {
if(0==j%2)
break; //如果是双数,直接结束循环
System.out.println(j);
}
}
}
- 练习-百万富翁
假设你月收入是3000,除开平时花销,每个月留下1000块钱进行投资;然后你认真的钻研了 《股票和基金 21天从入门到精通》,达到了每年20%的投资回报率。
那么问题来了,以每个月投资1000块钱的节奏,持续投资多少年,总收入达到100万(复利计算按照每年12000投入计算,不按照每月计息)。
复利公式:F = p* ( (1+r)^n );F 最终收入、p 本金、r 年利率、n 存了多少年。
**假设情景一:*p = 10000、r = 0.05、n = 1 本金是10000、年利率是5%、存了一年 1次,复利收入 10000( (1+0.05)^1 ) = 10500。
**假设情景二:*p = 10000、r = 0.05、n = 2。 本金是10000、年利率是5%、存了两年,复利收入 10000( (1+0.05)^2 ) = 11025。
public class Main {
public static void main(String[] args) {
int year = 0;
double sum = 0;
while(true){
sum += 12000;
year += 1;
sum = sum*(1+0.2);
System.out.println(sum);
if(sum > 1000000)
break;
}
System.out.println("需要 " + year +" 年才能攒够100万");
}
}
控制流程 结束外部循环
break是结束当前循环
- 使用boolean变量结束外部循环
借助boolean变量结束外部循环,需要在内部循环中修改这个变量值。每次内部循环结束后,都要在外部循环中判断,这个变量的值。
public class HelloWorld {
public static void main(String[] args) {
boolean breakout = false; //是否终止外部循环的标记
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.println(i + ":" + j);
if (0 == j % 2) {
breakout = true; //终止外部循环的标记设置为true
break;
}
}
if (breakout) //判断是否终止外部循环
break;
}
}
}
- 使用标签结束外部循环
在外部循环的前一行,加上标签;在break的时候使用该标签,即能达到结束外部循环的效果。
public class HelloWorld {
public static void main(String[] args) {
//打印单数
outloop: //outloop这个标示是可以自定义的比如outloop1,ol2,out5
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.println(i+":"+j);
if(0 == j%2)
break outloop; //如果是双数,结束外部循环
}
}
}
}