循环结构while与do-while和与程序调试debug

1. 什么是while循环结构

举例:

2.分析需求实现代码框架

在实际应用中,我们往往只会接到需求的文字概述,而非专业术语,也没有练习中的输出结果预览,此时我们需要学会分析问题,剖析问题中的逻辑思维,依靠剖析出的结果来实现对代码的大体架构,下面是简单的分析过程:

3. 什么是do-while循环结构

举例:

那么有了while循环为什么还需要do-while循环呢?下面是简单的举例:

由此可见,如果判断的值需要先执行代码来获得结果,那么就需要do-while循环先来输出结果,然后对结果进行判断,判断后再确认是否继续循环。两者的区别就是:

4. 如何进行程序调试debug(重点)

1.点击此处设置断点

2.debug该程序

3.运行下一步

4.继续运行下一步,按要求执行(此处输入数值)

4.继续运行下一步

蓝色表示现在运行的代码,斜体字表示运行中的数据。

5.根据debug结果调试程序

在此处结束

程序debug在今后检查代码有着极其重要的作用,它可以帮助我们寻找到程序中的错误以及数据的变化。

5.总结

课堂练习:

1.如图
public class loop_ke4 {
    public static void main(String[] args) {
        double student = 25;
        int year = 2012;
        while (student <= 100){
            student = student * 1.25;
            year++;
        }
        System.out.println((int)student+"    "+year);
    }
}

2.计算100以内(包括100)的偶数之和
public class loop_ke5 {
    public static void main(String[] args) {
        int num = 0;
        int total = 0;
        while(num<=100){
            if (num%2==0){
                total = total+num;
                System.out.println(total+"  "+num);
            }
            num++;
        }
    }
}

3.如图

import java.util.Scanner;

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

        Scanner scanner=new Scanner(System.in);
        int no;
        String ans="";
        while (!"n".equals(ans)) {
            System.out.print("请输入购买商品编号");
            no=scanner.nextInt();
            switch (no){
                case 1:
                    System.out.println("T恤");
                    break;
                case 2:
                    System.out.println("网球鞋");
                    break;
                case 3:
                    System.out.println("网球拍");
                    break;
            }
            System.out.print("是否继续(y/n)");
            ans=scanner.next();

        }
        if ("n".equals(ans)){
            System.out.println("程序结束!");
        }
    }
}

4.使用do-while实现:输出摄氏温度与华氏温度的对照表,要求它从摄氏温度0度到250度,每隔20度为一项,对照表中的条目不超过10条。

    转换关系:华氏温度 = 摄氏温度 * 9 / 5.0 + 32

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

        double tempC=0;
        double tempF;
        int line=1;

        System.out.println("Line    tempC   tempF");

        do {
            tempF = tempC*9/5.0+32;
            System.out.println(line+" "+tempC+" "+tempF);
            line++;
            tempC = tempC+20;
        }while (line<=10 && tempC<=250);
    }
}
5.如图

import java.util.Scanner;

public class ke3 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);

        System.out.println("1 $240");
        System.out.println("2 $120");
        System.out.println("3 $90");

        int goods;
        int count;
        double price =0;
        double total =0;
        String yn;

        do {
            System.out.print("which one you wanna buy:");
            goods=input.nextInt();
            switch (goods) {
                case 1:
                    price = 240;
                    break;
                case 2:
                    price = 120;
                    break;
                case 3:
                    price = 90;
                    break;
            }
            System.out.print("How many you wanna buy:");
            count=input.nextInt();
            total= total+price*count;

            System.out.print("continue?");
            yn = input.next();
        }while (!yn.equals("n"));

        System.out.println("Total:"+total);
    }
}

课后练习:

1.使用 while 循环,让用户输入密码,直到输入的密码正确为止。假设正确密码是 "123456"。

import java.util.Scanner;

public class loop_test6 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("请输入密码:");
        String pwd = input.next();

        while (!pwd.equals("123456")){
            System.out.println("密码错误,请重新输入。");
            System.out.print("请输入密码:");
            String pwd1 = input.next();
            pwd = pwd1;
        }

        System.out.println("密码正确,登录成功!");
    }
}

2. 猜数字游戏

题目描述:

程序随机生成一个 1 到 100 之间的整数,使用 while 循环让用户猜测该数字,若猜的数字与生成的数字不同,提示用户猜大了或猜小了,直到猜对为止。用while 循环实现相同的猜数字游戏。

期望输出:用户猜对数字后输出猜对的提示信息

import java.util.Scanner;

public class loop_test7 {
    public static void main(String[] args) {
        int answer = (int)(Math.random()*100);
        System.out.println("预知答案:"+answer);
        Scanner scanner = new Scanner(System.in);
        int userAns =-5;

        while(userAns != answer){

            System.out.print("请输入数字:");
            int userAns1 = scanner.nextInt();
            userAns = userAns1;
            if (userAns>answer){
                System.out.println("你猜大了!");
            }else if (userAns<answer){
                System.out.println("你猜小了!");
            }
        }

        if (userAns == answer){
            System.out.println("恭喜你猜中了!");
        }
    }
}

3.不断输入学生的考试分数,直到输入 -1 为止,使用 while 循环统计及格(分数大于等于 60)的学生人数。
import java.util.Scanner;

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

        Scanner input=new Scanner(System.in);

        double score = 0;
        int member = 0;

        while (score != -1){
            System.out.print("输入学生成绩(-1停止)");
            score = input.nextDouble();
            if (score>=60){
                member++;
            }
        }
        System.out.println("及格人数为"+member);
    }
}

4.输入学生的考试分数,使用 do - while 循环统计 0 - 59、60 - 79、80 - 100 这三个分数段的学生人数,输入 -1 结束输入。

public class test2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num1=0;
        int num2=0;
        int num3=0;
        double score;
        do {
            System.out.print("请输入学生分数(输入-1结束)");
            score = input.nextDouble();
            if (score>=80){
                num1++;
            }else if (score>=60){
                num2++;
            }else if (score>=0 && score<=59){
                num3++;
            }
        }while (score!=-1);
        System.out.println("80-100分的学生人数:"+num1);
        System.out.println("60-79分的学生人数:"+num2);
        System.out.println("0-59分的学生人数:"+num3);
    }
}

5.模拟员工打卡系统,员工每天打卡记录上班和下班时间(用整数表示,如 9 点上班,18 点下班)。使用 while 循环,不断输入上班和下班时间,直到输入 -1 为止,统计员工的总工作时长。

import java.util.Scanner;

public class test4 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);

        double start = 25;
        double end = 25;
        double time = 0;

        while (start != -1){
            System.out.print("请输入上班时间(输入-1结束):");
            start = input.nextDouble();
            if (start == -1){
                break;
            }
            System.out.print("请输入下班时间(输入-1结束):");
            end = input.nextDouble();

            time = time+(end-start);
        }
        System.out.println("员工工作总时长为:"+time+"小时");

    }
}

6.计划一次旅游,有多种开销项目(如交通、住宿、餐饮等)。使用 do - while 循环,输入每个开销项目的金额,直到输入 -1 为止,计算旅游的总预算,并判断是否超过 10000 元。

import java.util.Scanner;

public class test5 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);

        double money;
        double sum = 0;

        do {
            System.out.print("请输入开销项目金额(输入-1结束):");
            money = input.nextDouble();

            if (money == -1){
                break;
            }

            sum = sum + money;

        }while(money != -1);

        System.out.print("旅游总预算是:"+sum+"元,");

        if (sum>10000){
            System.out.print("不在预算内");
        }else {
            System.out.print("在预算范围内");
        }
    }
}

难度题:

1.家庭每月有固定收入,需要支付房租、水电费、食品费、交通费等各种费用。用户输入每月收入以及各项费用,程序检查预算是否超支。如果超支,计算超支金额,并通过减少非必要开支(如娱乐费用)来调整预算,直到不超支为止。假设娱乐费用可以每次减少 10%,每次调整后输出新的预算情况。

import java.util.Scanner;

public class ke_plus1 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);

        System.out.println("输入每月收入:");
        double budget = input.nextDouble();
        String name;
        double money;
        double total = 0;
        double entertainment = 0;

        do{
            System.out.println("输入费用项目:(输入“结束”停止输入,非必要开支均为《娱乐费用》):");
            name = input.next();

            if (name.equals("结束")){
                break;
            }

            System.out.println("输入费用金额:");
            money = input.nextDouble();

            total = total + money;

            if (name.equals("娱乐费用")){
                entertainment = entertainment + money;
            }

        }while (!name.equals("结束"));

        double diffVal = budget - total;
        double totalExcept = total - entertainment;

        while(diffVal<0){

            if(totalExcept>=budget){
                entertainment = 0;
                System.out.println("无法平衡预支。");
                System.out.println("当前娱乐费用为:"+(int)entertainment+"元");
                total = totalExcept;
                break;
            }

           System.out.println("当前总费用为:"+(int)total+"元,预算不足,削减10%娱乐费用,当前娱乐费用为:"+(int)entertainment+"元");
            entertainment = entertainment * 0.9;
            total = totalExcept + entertainment;
            diffVal = budget -total;

            if (diffVal>=0){
                System.out.println("当前娱乐费用为:"+(int)entertainment+"元");
                System.out.println("预算平衡,无需调整");
                break;
            }
        }

        System.out.println("总费用为:"+(int)total+"元,收入:"+budget+"元");

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值