循环结构的学习

简介: 循环结构的学习

为什么要学习循环

在编写代码时,业务需求(项目要求)原地打转,符合条件结束循环。
马拉松 (操场

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);
    }
}

dowhile循环

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);
    }
}

偶数之和

学员操作一计算100以内的偶数之和
训练要点
while循环结构
程序调试
需求说明
编程实现: 计算100以内 (包括100) 的偶数之和设置断点并调试程序,观察每一次循环中变量值的变化

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);
    }
}

断点调试

购物结算

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("程序结束!");
    }
}

循环的选择

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("程序结束!");
    }
}

类名和全类名

摄氏华氏对照表

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);
    }
}

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执行次序

五门功课成绩

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的特殊写法

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);
        }
    }
}

break和continue

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("最后");
    }
}
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使代码优雅

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("程序结束!");
    }
}

使用continue

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("程序结束!");
    }
}

小数的比较不能用==或!=

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("程序结束!");
    }
}

相关文章
|
5月前
|
C++
c++循环结构讲解
c++循环结构讲解
77 0
|
1月前
|
C#
C# 一分钟浅谈:循环结构 for 和 while 的应用
【9月更文挑战第1天】循环结构是编程中的基础概念,C# 中常用的 `for` 和 `while` 循环允许重复执行代码直至满足特定条件。`for` 循环基于计数,适用于已知循环次数的情况;`while` 循环基于条件,适用于未知循环次数的情况;`do-while` 循环则至少执行一次循环体。本文详细介绍了这些循环的语法和示例,并探讨了常见问题及其解决方法,如循环条件和更新表达式的错误设置。通过综合应用示例(如计算阶乘和斐波那契数列),帮助读者更好地理解和掌握循环结构的使用方法。正确使用循环可以提高程序的效率和可读性。
59 2
|
4月前
|
C++
C++循环结构
C++循环结构
|
5月前
第6章循环结构(二)
第6章循环结构(二)
|
5月前
|
C++
12循环结构
12循环结构
41 0
|
5月前
|
安全 程序员 C#
C#循环结构
C#循环结构
94 0
|
5月前
|
算法 C++
C++009-C++循环结构while
C++009-C++循环结构while
|
5月前
|
机器学习/深度学习 资源调度 物联网
C++系列-第3章循环结构-29-累乘和连除
C++系列-第3章循环结构-29-累乘和连除
|
5月前
|
算法 C++
C++007-C++循环结构
C++007-C++循环结构
|
11月前
|
前端开发 Python
选择与循环结构案例分享(5-1)
选择与循环结构案例分享(5-1)
52 0