java07循环结构

public class While02 {

    public static void main(String[] args) {

        /**
         * while(循环条件){
         *   循环体(循环操作)
         * }
         * 循环条件 必须是一个boolean类型的值!
         * 
         * 当满足了循环条件,会执行循环体,直到不满足循环条件是退出!
         */
        System.out.println("大家辛苦了1");
        System.out.println("大家辛苦了2");
        System.out.println("大家辛苦了3");
        System.out.println("大家辛苦了4");
        System.out.println("大家辛苦了5");
        System.out.println("**************************************");
        // 定义一个变量 用来保存 循环的次数
        int num = 0;
        while (num <= 10000) {
            System.out.println("大家辛苦了" + num);
            // 自身+1
            num++;
        }
        System.out.println(num);

    }

}
public class WhileDemo03 {

    public static void main(String[] args) {

        /**
         * 循环打印50份试卷
         * 分析:
         * 循环条件:count<=50  小于等于50次
         * 循环体: 打印试卷
         */
        int count = 1;
        while (count <= 50) {
            System.out.println("正在打印第" + count + "份试卷");
            // 迭代变量
            count++;
        }
    }
}
public class WhileDemo04 {

    /**
     * 老师每天检查张浩的学习任务是否合格。
     * 如果不合格,则继续进行。
    老师给张浩安排的每天的学习任务为:
    上午阅读教材,学习理论部分,
    下午上机编程,掌握代码部分
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入是否合格?(y/n)");
        String answer = scanner.next();
        // 循环条件 -==》是否合格? 如果不合格 一直学习!
        while (answer.equalsIgnoreCase("n")) {
            System.out.println("阅读教材,学习理论部分");
            System.out.println("上机编程,掌握代码部分");
            System.out.println("再次输入是否合格?(y/n)");
            answer = scanner.next();
        }
        System.out.println("完成了 学习任务!");
    }

}
public class WhileDemo05 {

    /**
     * 2012年培养学员25万人,每年增长25%。
     * 请问按此增长速度
     * 到哪一年培训学员人数将达到100万人?
     */
    public static void main(String[] args) {

        int year = 2012;
        double person = 25;
        while (person <= 100) {
            person *= (1 + 0.25); // person*=(1+0.25);
            year++;
        }
        System.out.println(year + "培训学员人数将达到100万人");
    }
}
public class WhileDemo06 {

    /**
     * 编程实现:计算100以内(包括100)的偶数之和
    设置断点并调试程序,观察每一次循环中变量值的变化
     */
    public static void main(String[] args) {

        int num = 0;
        int sum = 0;// 记录所有的偶数和
        while (num <= 100) {
            if (num % 2 == 0) { // 偶数
                sum += num; // sum=sum+num
            }
            num++;
        }
        System.out.println("所有的偶数和:" + sum);
    }
}
public class WhileDemo07 {
    public static void main(String[] args) {
        System.out.println("欢迎进入MyShopping管理系统");
        System.out.println("***************************");
        System.out.println("1.帽子 	 2.网球鞋 	 3.球拍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请您输入购买物品的编号");
        int choose = scanner.nextInt(); // 获取用户的选择
        System.out.println("请您输入购买的数量");
        int count = scanner.nextInt();// 获取用户购买的数量
        // 定义一个总消费
        double money = 0;
        switch (choose) {
        case 1:
            System.out.println("您购买的是 1.帽子 	 ¥50	总金额:" + (count * 50));
            money += (count * 50);
            break;
        case 2:
            System.out.println("您购买的是 2.网球鞋  	 ¥30	总金额:" + (count * 30));
            money += (count * 30);
            break;
        case 3:
            System.out.println("您购买的是 3.球拍 	 ¥5	总金额:" + (count * 5));
            money += (count * 5);
            break;
        }
        System.out.println("是否继续购物?(y/n)");
        String answer = scanner.next();

        while (answer.equalsIgnoreCase("y")) { // 循环购物操作
            System.out.println("***************************");
            System.out.println("1.帽子 	 2.网球鞋 	 3.球拍");
            System.out.println("请您输入购买物品的编号");
            choose = scanner.nextInt(); // 获取用户的选择
            System.out.println("请您输入购买的数量");
            count = scanner.nextInt();// 获取用户购买的数量
            switch (choose) {
            case 1:
                System.out.println("您购买的是 1.帽子 	 ¥50	总金额:" + (count * 50));
                money += (count * 50);
                break;
            case 2:
                System.out.println("您购买的是 2.网球鞋  	 ¥30	总金额:" + (count * 30));
                money += (count * 30);
                break;
            case 3:
                System.out.println("您购买的是 3.球拍 	 ¥5	总金额:" + (count * 5));
                money += (count * 5);
                break;
            }
            System.out.println("是否继续购物?(y/n)");
            answer = scanner.next();
        }
        System.out.println("您本次消费是:" + money);
        System.out.println("请您输入支付金额:");
        double pay = scanner.nextDouble();
        while (pay < money) { // 说明金额不正确
            System.out.println("金额不正确!请重新支付!");
            pay = scanner.nextDouble();
        }
        System.out.println("找零:" + (pay - money));
    }
}
public class DoWhileDemo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double money = 0;
        String answer = "";
        do { // 先执行 再判断
            System.out.println("欢迎进入MyShopping管理系统");
            System.out.println("***************************");
            System.out.println("1.帽子 	 2.网球鞋 	 3.球拍");
            System.out.println("请您输入购买物品的编号");
            int choose = scanner.nextInt(); // 获取用户的选择
            System.out.println("请您输入购买的数量");
            int count = scanner.nextInt();// 获取用户购买的数量
            switch (choose) {
            case 1:
                System.out.println("您购买的是 1.帽子 	 ¥50	总金额:" + (count * 50));
                money += (count * 50);
                break;
            case 2:
                System.out.println("您购买的是 2.网球鞋  	 ¥30	总金额:" + (count * 30));
                money += (count * 30);
                break;
            case 3:
                System.out.println("您购买的是 3.球拍 	 ¥5	总金额:" + (count * 5));
                money += (count * 5);
                break;
            }
            System.out.println("是否继续购物?(y/n)");
            answer = scanner.next();
        } while (answer.equalsIgnoreCase("y"));

        System.out.println("您本次消费是:" + money);
        System.out.println("请您输入支付金额:");
        double pay = scanner.nextDouble();
        while (pay < money) { // 说明金额不正确
            System.out.println("金额不正确!请重新支付!");
            pay = scanner.nextDouble();
        }
        System.out.println("找零:" + (pay - money));
    }
}
public class DoWhileDemo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double money = 0; // 记录总消费
        String answer = "";
        // 定义一个变量 来记录消费清单
        String bill = "";
        do { // 先执行 再判断
            System.out.println("欢迎进入MyShopping管理系统");
            System.out.println("***************************");
            System.out.println("1.帽子 	 2.网球鞋 	 3.球拍");
            System.out.println("请您输入购买物品的编号");
            int choose = scanner.nextInt(); // 获取用户的选择
            System.out.println("请您输入购买的数量");
            int count = scanner.nextInt();// 获取用户购买的数量
            switch (choose) {
            case 1:
                String x = "您购买的是 1.帽子 	 ¥50	总金额:" + (count * 50);
                System.out.println(x);
                bill += x + "
";
                money += (count * 50);
                break;
            case 2:
                String x2 = "您购买的是 2.网球鞋  	 ¥30	总金额:" + (count * 30);
                System.out.println(x2);
                bill += x2 + "
";
                money += (count * 30);
                break;
            case 3:
                String x3 = "您购买的是 3.球拍 	 ¥5	总金额:" + (count * 5);
                System.out.println(x3);
                bill += x3 + "
";
                money += (count * 5);
                break;
            }
            System.out.println("是否继续购物?(y/n)");
            answer = scanner.next();
        } while (answer.equalsIgnoreCase("y"));

        System.out.println("您本次消费是:" + money);
        System.out.println("请您输入支付金额:");
        double pay = scanner.nextDouble();
        while (pay < money) { // 说明金额不正确
            System.out.println("金额不正确!请重新支付!");
            pay = scanner.nextDouble();
        }
        System.out.println("是否需要消费清单:(y/n)");
        if (scanner.next().equalsIgnoreCase("y")) {
            System.out.println(bill);
        }
        System.out.println("找零:" + (pay - money));
    }
}
public class WhileDemo08 {

    /**
     * 使用do-while实现:输出摄氏温度与华氏温度的对照表,
     * 要求它从摄氏温度0度到250度,每隔20度为一项,对照表中的条目不超过10条。
    转换关系:华氏温度 = 摄氏温度 * 9 / 5.0 + 32

    循环操作:计算摄氏温度,并输出对照条目
    循环条件:
    条目<=10 && 摄氏温度 <= 250
     */
    public static void main(String[] args) {
        int count = 1; // 对照条目
        double c = 0; // 摄氏度
        double f = 0; // 华氏度
        System.out.println("条目	摄氏度	华氏度");
        do {
            // 转换
            f = c * 9 / 5.0 + 32;
            System.out.println(count + "	" + c + "	" + f);
            count++;
            c += 20;
        } while (count <= 10 && c <= 250);

    }

}
public class WhileDemo09 {

    /**
     * 实现一个数字的反转
     */
    public static void main(String[] args) {
        int num = 123456789;
        int temp = 1;
        System.out.print("反转之后的数字:");
        while (num != 0) {
            // 依次与10取余
            temp = num % 10; // 6
            System.out.print(temp);
            num = num / 10;
        }
    }

}
public class ForDemo01 {

    public static void main(String[] args) {
        /**
         * 
         * for(表达式1;表达式2;表达式3) {
         *     循环体
         * }
         * 表达式1:初始化变量  int a=0;
         * 表达式2:循环条件!满足条件执行循环体操作!
         * 表达式3:迭代变量!
         * 
         * 三个表达式都可以省略!但是;不能省略!
         * 
         * 
         * 执行顺序:
         *  表达式1---》表达式2---》循环体---》表达式3--->表达式2---》循环体---》表达式3
         */
        for (int i = 1; i <= 100; i++) {
            System.out.println("好好学习!" + i);
        }
    }
}
public class ForDemo02 {
    /**
     * 循环输入某同学S1结业考试的5门课成绩,并计算平均分
     */
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("请您输入姓名:");
        // 定义一个变量 来保存总成绩
        double sum = 0;
        String name = scanner.next();
        for (int i = 1; i <= 5; i++) {
            System.out.println("请您输入第" + i + "门课程的成绩:");
            double score = scanner.nextDouble();
            sum += score; // 总成绩
        }
        System.out.println("平均分是:" + (sum / 5));

    }

}
public class ForDemo03 {
    public static void main(String[] args) {
        // 根据用户的输入 产生对应的加法表
        Scanner scanner = new Scanner(System.in);
        System.out.println("请您输入一个数字:");
        int num = scanner.nextInt();
        for (int i = 0; i <= num; i++) {
            System.out.println(i + "+" + (num - i) + "=" + (num));
        }

    }

}
public class ErrorDemo04 {
    public static void main(String[] args) {
        /**
         * int i = 0; // 把初始化变量 局部化!
        for (; i <= 5; i++) {
            System.out.println(i);

        }
        */
        /**for (;;) { // 死循环
            System.out.println(1);
        }*/

        /**for (int i = 0;; i++) { // 省略了 条件判断
            System.out.println(i);
        }
        */

        for (int i = 0; i < 10;) { // 省略了 条件判断迭代变量
            System.out.println(i);
        }

    }
}
public class ForDemo05 {
    public static void main(String[] args) {
        int sum = 0;
        // 求1~100之间不能被3整除的数之和
        for (int i = 1; i <= 100; i++) {
            // 不能被3整除的数
            if (i % 3 != 0) {
                sum += i;
            }
        }
        System.out.println("不能被3整除的数之和:" + sum);
    }

}
public class ForDemo06 {
    // 计算年龄大于30的 占比
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 定义年龄的变量
        int age = 0;
        // 年龄大于30以上的人数
        int count = 0;
        for (int i = 1; i <= 5; i++) {
            System.out.println("请您输入年龄:");
            age = scanner.nextInt();
            if (age >= 30) {
                count++;
            }
        }
        System.out.println("30岁以上的比例是:" + (count / 5.0 * 100) + "%");
        System.out.println("30岁以下的比例是:" + ((1 - count / 5.0) * 100) + "%");
    }

}
public class ForDemo07 {
    public static void main(String[] args) {

        System.out.println("请您输入一个数字:");
        Scanner scanner = new Scanner(System.in);
        double num = scanner.nextDouble();

        DecimalFormat df = new DecimalFormat("0.00"); // 保留两位有效数字
        String number = df.format(num); // 把double类型的数据 保留两位有效数字 返回String
        System.out.println("保留两位有效数字:" + number);

        System.out.println("*********************************");

        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(2); // 保留两位有效数字
        number = nf.format(num);
        System.out.println("保留两位有效数字:" + number);

    }

}
public class ForDemo08 {
    /**
     * 日期的转换
     * @throws ParseException 
     */
    public static void main(String[] args) throws ParseException {

        Date date = new Date(); // 日期 格式 Mon Mar 06 11:02:55 CST 2017
        System.out.println("日期 格式:" + date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年-MM月-dd日 hh:mm:ss");
        String time = sdf.format(date); // 把日期格式 转换成 String
        System.out.println(time);
        time = "2018年-05月-05日 11:11:11";
        // 把 String 转换成 日期格式
        System.out.println(sdf.parse(time));
    }

}
public class ForDemo09 {
    //break  跳出当前循环结构
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            System.out.println("第" + i + "圈");
            if (i == 8) {
                System.out.println("坚持不住!!!");
                break;
            }
        }
        System.out.println("比赛结束!");
    }
}
public class ForDemo10 {
    /**
     * 循环录入某学生5门课的成绩并计算平均分。
     * 如果某分数录入为负,停止录入并提示录入错误
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 用来保存总成绩
        double sum = 0;
        // 标记 是否计算平均分
        boolean flag = true;
        for (int i = 1; i <= 5; i++) {
            System.out.println("请您输入第" + i + "门课程的成绩:");
            double score = scanner.nextDouble();
            if (score < 0) { // 分数为负数
                System.out.println("您的输入有误!退出系统!");
                flag = false;
                break;
            }
            sum += score;
        }
        if (flag) { // flag==true
            System.out.println("平均分是:" + (sum / 5));
        }

    }
}
public class ForDemo11 {

    /**
     *1~10之间的整数相加,得到累加值大于20的当前数
     */
    public static void main(String[] args) {

        // 定义变量保存 累加值
        int sum = 0;
        for (int i = 0; i <= 10; i++) {
            sum += i;
            if (sum > 20) {
                break; // 跳出当前循环结构!
            }
        }
        System.out.println("当前的sum===:" + sum);

    }

}
public class ForDemo12 {

    /**
     *循环录入Java课的学生成绩,
     *统计分数大于等于80分的学生比例
     *
     *continue:代表的是:结束本次循环!继续下次的循环!  之后的代码不会执行!
     *跳出循环体了吗??没有跳出!代表 循环继续!
     *
     *break: 跳出当前整个循环体!
     *
     *return:  跳出当前方法,可以带有返回值!
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 定义变量 保存分数大于80的人数
        int sum = 0;
        System.out.println("请输入班级的人数:");
        int count = scanner.nextInt();
        for (int i = 1; i <= count; i++) {
            System.out.println("请输入第" + i + "名同学的成绩:");
            double score = scanner.nextDouble();
            if (score < 80) {
                continue;
            }
            sum++;
            return;
        }
        System.out.println("80分以上的人数:" + sum);
        System.out.println("80分以上的人数占比:" + (sum * 1.0 / count) * 100 + "%");
    }
}
public class ForDemo13 {

    /**
     *验证用户登录  失败次数
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String name; // 用户名
        String password; // 密码
        int i;// 记录用户失败的次数
        // 进行三次循环操作
        for (i = 0; i < 3; i++) {
            System.out.println("请输入用户名:");
            name = scanner.next();
            System.out.println("请输入密码:");
            password = scanner.next();
            // 判断用户名和密码是否正确
            if ("admin".equals(name) && "admin".equals(password)) {
                System.out.println("登录成功!");
                break;
            } else {
                System.out.println("输入错误!您还有" + (2 - i) + "次机会!");
            }
        }
        if (i == 3) { // 3次输入都不对
            System.out.println("对不起!您三次都输入错误!");
        }

    }
}
// 三种循环 来实现 100以内的偶数和
    public static void main(String[] args) {
        // 01.使用while循环
        int num = 0;
        int sum = 0; // 求和
        while (num <= 100) {
            // 找到偶数并相加
            if (num % 2 == 0) {
                sum += num; // sum=sum+num??
            }
            num++;
        }
        System.out.println("while偶数和是:" + sum);

        // 02.使用do while
        sum = 0;
        num = 0;
        do {
            if (num % 2 == 0) { // 找到偶数
                sum += num;
            }
            num++;
        } while (num <= 100);
        System.out.println("dowhile偶数和是:" + sum);

        // 03.使用for
        sum = 0;
        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                sum += i;
            }
        }
        System.out.println("for偶数和是:" + sum);

    }
三种循环获取100以内的偶数和
// a+=b a=a+b 是一致的吗?
        int a = 5;
        double b = 5;
        // a =a + b;
        a += b; // 做了强制类型转换 (int) (a + b) 不会编译报错
        System.out.println(a);
a+=b与 a=a+b
原文地址:https://www.cnblogs.com/999-/p/6109528.html