循环结构:for
案例一:输出三行 Hello World
案例二:输出100以内的奇数
案例三:输入一个数n,输出1 ~ n 的数据和
package com.HAF.hello;
import java.util.Scanner;
// 循环结构:for循环
public class For_Test {
public static void main(String[] args) {
test1();
test2();
test3();
}
// 输出三行:Hello World
/* 执行流程:
1. 循环一开始,执行int i = 0 一次。
2. 此时 i = 0,执行 System.out.println("Hello World") 一次。
3. 执行 i++,此时 i = 1。
4. 执行 i < 3,此时 i = 1,1 < 3,为true,执行循环体。
5. 执行 System.out.println("Hello World") 一次。
6. 执行 i++,此时 i = 2。
7. 执行 i < 3,此时 i = 2,2 < 3,为true,执行循环体。
8. 执行 System.out.println("Hello World") 一次。
9. 执行 i++,此时 i = 3。
10. 执行 i < 3,此时 i = 3,3 < 3,为false,不执行循环体。
11. 循环结束。
*/
public static void test1() {
for (int i = 0; i < 3; i++) {
System.out.println("Hello World");
}
}
// 输入100以内的奇数
public static void test2() {
for (int i = 1; i < 100; i++) {
if (i % 2 != 0) {
System.out.println(i);
}
}
}
// 输入一个数n,计算出1到n的数据和
public static void test3() {
System.out.println("请输入一个数:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
System.out.println("1到" + n + "的数据和是:" + sum);
}
}
循环结构:while
- 假设你在银行投资了100000元,银行给出的复利是1.7%,问多少年后能实现本金翻倍?
// while学习
public class While_Test {
public static void main(String[] args) {
test1();
System.out.println("需要"+ test2() + "年才能实现本金翻倍");
}
// 假设你在银行投资了100000元,银行给出的复利是1.7%,问多少年后能实现本金翻倍?
public static void test1() {
double money = 100000; // 本金
int year = 0; // 年份
double rate = 0.017; // 利率
while (money < 200000) {
year++;
money = money * (1 + rate);
}
System.out.println(year + "年后能实现本金翻倍");
}
public static int test2() {
double money = 100000; // 本金
int year = 0; // 年份
double rate = 0.017; // 利率
while (money < 200000) {
year++;
money = money * (1 + rate);
}
return year;
}
}
- 假如有一张足够大的纸,它的厚度是0.1毫米。请问:该纸张折叠多少次,可以折成珠穆朗玛峰的高度?【珠穆朗玛峰高度是:8848.86米=8848860毫米】
public class While_Test2 {
public static void main(String[] args) {
System.out.println("需要" + test1() + "次才能折成珠穆朗玛峰的高度");
}
// 假如有一张足够大的纸,它的厚度是0.1毫米。请问:该纸张折叠多少次,可以折成珠穆朗玛峰的高度?
public static int test1() {
double count = 0; // 折叠次数
double paper = 0.1; // 纸的厚度
double height = 8848860; // 珠穆朗玛峰的高度
while (paper < height) {
paper *= 2;
count++;
}
return (int)count;
}
}
循环结构:do-while
do-while循环的特点: 先执行后判断(至少会执行一次)
public class DoWhile_Test {
public static void main(String[] args) {
test1();
}
public static void test1() {
int i = 0;
do {
System.out.println("do...while循环");
i++;
} while (false);
System.out.println(i);
}
}
循环嵌套
案例一:九九乘法表
案例二:九九乘法表倒置
package com.HAF.hello;
// 循环嵌套
public class XunHuan_Test {
public static void main(String[] args) {
test1();
System.out.println("---------------------------------------------------");
test2();
}
// 打印九九乘法表嵌套循环
public static void test1() {
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();
}
}
// 打印九九乘法表倒置
public static void test2() {
for (int i = 9; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (i * j) + "\t");
}
System.out.println();
}
}
}