Java基础学习笔记-3

前言

本学习笔记将重点介绍Java中的循环结构、条件语句以及用户输入处理的重要概念和技巧。通过学习这些内容,您将能够更好地理解和利用Java来解决各种编程问题。
Java基础学习笔记-1
Java基础学习笔记-2

1. 循环结构

1.1 While 循环

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        /*
        while (条件) {
            循环的体(内容);
            更新条件;
        }
        1 +到 100,5050
         */
        int i = 1, sum = 0;
        while (i <= 100) {
    
    
            sum += i;
            i++;
        }
        System.out.println("和:" + sum);
    }
}

While 循环是一种常见的循环结构,它在满足条件的情况下重复执行一段代码块。在这个示例中,我们使用 While 循环计算从 1 到 100 的所有整数的和。

1.2 Do-While 循环

public class Demo02 {
    
    
    public static void main(String[] args) {
    
    
        /*
        do {
            循环的体(内容);
            更新条件;
        } while(条件);
        1 +到 100,5050
         */
        int i = 1, sum = 0;
        do {
    
    
            sum += i;
            i++;
        } while (i <= 100);
        System.out.println("和:" + sum);
    }
}

Do-While 循环与 While 循环类似,但它首先执行代码块,然后检查条件是否满足,至少会执行一次。在这个示例中,我们同样计算从 1 到 100 的所有整数的和。

1.3 While 循环与条件

public class Demo03 {
    
    
    public static void main(String[] args) {
    
    
        int n = 1, sum = 0;
        while (n <= 100) {
    
    
            if (n % 2 == 0) sum += n;
            n++;
        }
        System.out.println("sum: " + sum);
    }
}

这个示例演示了如何在 While 循环中使用条件语句。在这种情况下,我们计算从 1 到 100 之间的偶数的和。

2. 用户输入与条件语句

2.1 Switch 语句

import java.util.Scanner;

public class Demo04 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("购物结算");
        System.out.println("请选择商品编号:");
        System.out.println("1.T恤  2.网球鞋 3.网球拍");

        String jx = "y";
        while (jx.equals("y")) {
    
    
            System.out.print("请输入商品编号:");
            int select = scanner.nextInt();
            switch (select) {
    
    
                case 1:
                    System.out.println("T恤 100元");
                    break;
                case 2:
                    System.out.println("网球鞋 200元");
                    break;
                case 3:
                    System.out.println("网球拍 300元");
                    break;
            }
            System.out.print("是否继续(y/n):");
            jx = scanner.next();
        }

        System.out.println("程序结束!");
    }
}

Switch 语句用于根据不同的条件执行不同的代码块。在这个示例中,我们模拟了一个购物结算程序,根据用户输入的商品编号选择相应的商品并计算总价格。

2.2 Do-While 循环与用户输入

import java.util.Scanner;

public class Demo05 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("购物结算");
        System.out.println("请选择商品编号:");
        System.out.println("1.T恤  2.网球鞋 3.网球拍");

        String jx;
        do {
    
    
            System.out.print("请输入商品编号:");
            int select = scanner.nextInt();
            switch (select) {
    
    
                case 1:
                    System.out.println("T恤 100元");
                    break;
                case 2:
                    System.out.println("网球鞋 200元");
                    break;
                case 3:
                    System.out.println("网球拍 300元");
                    break;
            }
            System.out.print("是否继续(y/n):");
            jx = scanner.next();
        } while (jx.equals("y"));

        System.out.println("程序结束!");
    }
}

这个示例类似于前一个示例,但使用 Do-While 循环来持续提供用户选择商品的选项,直到用户选择退出。

2.3 使用 Do-While 循环输出温度对照表

public class Demo06 {
    
    
    public static void main(String[] args) {
    
    
        /*
            使用do-while实现:输出 *摄氏温度 与 *华氏温度的对照表,要求它从摄氏温度0度到250度,每隔20度为一项,对照表中的 *条目 不超过10条。
            转换关系:华氏温度 = 摄氏温度 * 9 / 5.0 + 3
         */
        double huashidu, sheshidu = 0;
        int count = 0;
        do {
    
    
            huashidu = sheshidu * 9 / 5.0 + 32;
            System.out.println(sheshidu + " vs. " + huashidu);
            sheshidu += 20;
            count++;
        } while (sheshidu <= 250 && count <= 10);
    }
}

这个示例演示了如何使用 Do-While 循环来输出摄氏温度与华氏温度的对照表。温度从摄氏温度 0 度递增到 250 度,每隔 20 度一项,并且对照表中的条目不超过 10 条。摄氏温度与华氏温度之间的转换关系是华氏温度 = 摄氏温度 * 9 / 5.0 + 32。

这个示例展示了如何使用 Do-While 循环来生成一个有限数量的温度对照表,同时计算温度之间的转换关系。

3. For 循环

3.1 For 循环基本用法

public class Demo07 {
    
    
    public static void main(String[] args) {
    
    
        /*
        1 +到 100,5050
        特点:循环次数固定下来的。建议使用for循环。
        for (声明初始化循环变量; 条件; 修改循环变量) {
            循环体;
        }
         */
        int sum = 0;
        for (int i=0; i<=100; i++) {
    
    
            sum += i;
        }
        System.out.println("和:" + sum);
    }
}

For 循环是一种循环结构,通常用于已知循环次数的情况。在这个示例中,我们使用 For 循环计算从 1 到 100 的所有整数的和。

3.2 For 循环与用户输入

import java.util.Scanner;

public class Demo08 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入姓名:");
        String name = scanner.next(); // 记录姓名
        int sum = 0; // 记录总成绩
        for (int i=1; i<=5; i++) {
    
    
            System.out.print("请输入5门功课的第" + i + "门的成绩:");
            sum += scanner.nextInt();
        }
        System.out.println(name + "的平均分是:" + sum / 5.0);
    }
}

这个示例演示了如何使用 For 循环来计算一个学生的平均分,其中学生需要输入五门课程的成绩。

3.3 多重变量的 For 循环

public class Demo09 {
    
    
    public static void main(String[] args) {
    
    
        for (int i=0,j=6; i<=6; i++,j--) {
    
    
            System.out.println(i + "+" + j + "=" + 6);
        }
    }
}

For 循环还可以使用多个变量进行控制,如在这个示例中,我们使用两个变量来生成一张加法表。

4. Break 和 Continue

4.1 使用 Break 结束循环

import java.util.Scanner;

public class Demo10 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int sum = 0;
        for (int i=1; i<=5; i++) {
    
    
            System.out.print("成绩:");
            int cj = scanner.nextInt();
            if (cj < 0) break; // 结束循环
        }

        System.out.println("最后");
    }
}

Break 语句用于立即结束循环。在这个示例中,我们根据用户输入的成绩,如果成绩为负数,则立即结束循环。

4.2 使用 Continue 跳过当前迭代

import java.util.Scanner;

public class Demo11 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int sum = 0;
        for (int i=1; i<=5; i++) {
    
    
            System.out.print("成绩:");
            int cj = scanner.nextInt();
            if (cj < 0) continue; // 忽略当次
            sum += cj;
        }

        System.out.println("和:" + sum);
    }
}

Continue 语句用于跳过当前迭代并继续下一次迭代。在这个示例中,我们忽略了用户输入的负数成绩。

5. 嵌套循环

5.1 嵌套循环示例

import java.util.Scanner;

public class Demo12 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("> 添加客户信息");
        for (int i=1; i<=3; i++) {
    
    
            System.out.print("会员号《4位》:");
            int id = scanner.nextInt();
            if (id >= 1000 && id <= 9999) {
    
     // 满足条件1
                System.out.print("生日《mm/dd》:");
                String birth = scanner.next();
                if (birth.length() == 5) {
    
     // 满足条件2
                    System.out.print("积分:");
                    int jifeng = scanner.nextInt();
                    if (jifeng >= 0) {
    
     // 满足条件3
                        System.out.println("会员信息是:\n" + id + "\t" + birth + "\t" + jifeng);
                    }
                }
            }
        }

        System.out.println("程序结束!");
    }
}

嵌套循环是一种在循环内部包含另一个循环的结构。在这个示例中,我们模拟了添加客户信息的过程,需要满足多个条件。

5.2 嵌套循环优化

import java.util.Scanner;

public class Demo13 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        System.out.println("> 添加客户信息");
        for (int i=1; i<=3; i++) {
    
    
            System.out.print("会员号《4位》:");
            int id = scanner.nextInt();
            if (id < 1000 || id > 9999) continue; // 过滤
            System.out.print("生日《mm/dd》:");
            String birth = scanner.next();
            if (birth.length() != 5) continue; // 过滤
            System.out.print("积分:");
            int jifeng = scanner.nextInt();
            if (jifeng < 0) continue; // 过滤
            System.out.println("会员信息是:\n" + id + "\t" + birth + "\t" + jifeng);
        }

        System.out.println("程序结束!");
    }
}

这个示例展示了如何使用 continue 语句来优化嵌套循环,过滤掉不符合条件的输入。

6. 浮点数比较

6.1 浮点数比较

import java.util.Scanner;

public class Demo14 {
    
    
    public static void main(String[] args) {
    
    
        final double JINDU = 0.00001;
        /*
        1. 录入一个小数1,小数2,小数3。
            判断小数1+小数2是否等于小数3?
            小数是模拟出来的数,近似值。无法用== !=进行比较的。
         */
        Scanner scanner = new Scanner(System.in);
        System.out.print("小数1:");
        double d1 = scanner.nextDouble();
        System.out.print("小数2:");
        double d2 = scanner.nextDouble();
        System.out.print("小数3:");
        double d3 = scanner.nextDouble();
        if (d1 + d2 <= d3+JINDU && d1 + d2 >= d3-JINDU) System.out.println("d1+d2==d3");
        else System.out.println("d1+d2!=d3");

        System.out.println("程序结束!");
    }
}

由于浮点数的近似性质,直接使用 == 进行比较可能会导致问题。在这个示例中,我们演示了如何使用一个小的常量来比较浮点数。

总结

通过本学习笔记,我们深入了解了Java编程语言中循环结构、条件语句和用户输入处理的关键概念。这些基础知识对于编写功能强大且可靠的Java应用程序至关重要。

总结一下,学习重点包括:

  • 了解不同类型的循环结构,如While循环、Do-While循环和For循环,以及它们的适用场景。
  • 如何使用条件语句在循环中进行逻辑判断和筛选。
  • 处理用户输入的技巧,包括使用Switch语句来处理不同的用户选择。
  • 在循环中使用Break和Continue语句来控制循环的执行流程。
  • 嵌套循环的概念,以及如何使用它们来解决更复杂的问题。
  • 浮点数比较的方法,确保准确的数值比较。

这些知识将使您能够更加灵活地编写Java程序,有效地处理各种编程任务。希望这些学习笔记对您的Java编程之旅有所帮助,祝您编程愉快!

猜你喜欢

转载自blog.csdn.net/qq_42531954/article/details/132713734