循环
循环
1.分支结构
1.1 if结构
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入内容");
String s = scanner.nextLine();
if(s.equals("Hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}

1.2 if-else结构
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
int score = scanner.nextInt();
if (score>60){
System.out.println("及格");
}else {
System.out.println("不及格");
}
}
}

1.3 多重if结构
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩");
int score = scanner.nextInt();
if (score==100){
System.out.println("恭喜满分");
} else if (score<100&&score>=90) {
System.out.println("A级");
} else if (score<90&&score>=80) {
System.out.println("B级");
} else if (score<80&&score>=70) {
System.out.println("C级");
} else if (score<70&&score>=60) {
System.out.println("D级");
} else if (score<60&&score>=0) {
System.out.println("不及格");
}else {
System.out.println("成绩不合法");
}
}
}

1.4 switch结构
import java.util.Scanner;
public class Demo09 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请按键");
int order = scanner.nextInt();
switch (order){
case 1:
System.out.println("您进行的是归属地、套餐服务");
break;
case 2:
System.out.println("您进行的是优惠活动");
break;
case 3:
System.out.println("您进行的是停复机手机上网及密码服务");
break;
case 4:
System.out.println("您进行的是增值业务预定");
break;
case 5:
System.out.println("您进行的是无线宽带业务");
break;
case 0:
System.out.println("您进行的是即将转入人工服务");
break;
}
}
}

switch的运行规则:“先匹配(只一次)后贯穿执行,遇到break才跳出”。case内部可以嵌套使用if-else结构,甚至是另一个switch结构。
2 循环结构
2.1 while循环
public static void main(String[] args) {
int i = 1;
while (i<=100){
System.out.println("第"+i+"次说:我能行");
i++;
}
}

练习
2009年公司收入是100亿,假定每年增长25%,请问按此增长速度,到哪一年公司收入达到500亿元
方法1
public static void main(String[] args) {
int year = 2009;
double sum = 100;
while(true){
year++;
sum = 100*Math.pow(1.25,year-2009);
if (sum>500){
break;
}
}
System.out.println(year+"年的收入为"+sum+"超过500亿元");
}

Math.pow()是对次方的应用
方法2
public static void main(String[] args) {
int year = 2009;
double sum = 100;
while (sum<500){
sum*=1.25;
year++;
}
System.out.println(year+"年的收入为"+sum+"超过500亿元");
}

2.2 do-while循环
import java.util.Scanner;
public class Dome13 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = 0;
do {
System.out.println("0:退出 1:进货 2:销售 3:盘点");
m=scanner.nextInt();
switch (m){
case 1:
System.out.println("执行进货");
break;
case 2:
System.out.println("执行销售");
break;
case 3:
System.out.println("执行盘点");
break;
}
}while (m!=0);
System.out.println("欢迎下次使用");
}

因为是do-while结构,所以会首先输出功能菜单选择提示信息,然后使用switch结构来进行功能分发,do-while内部套switch是一种基本模式
2.3 for循环
用for循环输出1~100之间能被5整除的数,并且每行输出三个
public static void main(String[] args) {
//练习:用while或for循环输出1~100之间能被5整除的数,并且每行输出3个
for (int i = 0; i <= 100; i++) {
if (i%5==0){
System.out.print(i+"\t");
}
if (i%(5*3)==0){
System.out.println();
}
}
}

3 循环嵌套
3.1 输出长方形
public static void main(String[] args) {
//外层循环控制行数
for (int i = 1; i <= 3; i++) {
//内层循环控制列数
for (int j = 1; j <=8 ; j++) {
System.out.print("*");//注意事项print不是println
}
//每打印完一行进行换行
System.out.println();
}
}

执行流程:当i=1时,外层循环条件成立,进入内层循环,开始打印第一行内容。此时,j从1开始,循环8次,内层循环结束换行,实现第一行8个*的输出。接下来返回外层循环i变为2,准备打印下一行,以此类推,知道打印完成
3.2 输出九九乘法表
public static void main(String[] args) {
//打印九九乘法表
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
}
}

3.3 输出等腰三角形
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i ; j--) {
System.out.print(" ");
}
for (int j=1;j<=i;j++){
System.out.print("*");
}
for (int j=1;j<i;j++){
System.out.print("*");
}
System.out.println();
}
}

4 应用
4.1 小球问题--正向
一个小球从100m高度自由下落,每次落地后反跳回原高度的一半;再落下,求他在第第10次落地时,共经过多少m?第10次反弹多高?
public static void main(String[] args) {
double sum = 100,f=100;
for (int i = 2; i <= 10; i++) {
sum+=f;
f/=2;//经过这次运算后,得到一个新值,就是下次要累加的值
}
System.out.println("总距离"+sum);
double h = 50;
for (int i = 2; i <= 10; i++) {
h=h/2;
}
System.out.println("第10次的高度"+h);
}

4.2 猴子摘桃--逆向
猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个;第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少?
public static void main(String[] args) {
int f=1;
for (int i = 8; i >= 0; i--) {
f=(f+1)*2;
}
System.out.println("猴子摘了"+f+"个桃子");
}


浙公网安备 33010602011771号