首页 > 编程语言 > 详细

循环结构------>Java学习的第一个难点(while------do while)

时间:2017-04-05 12:36:13      阅读:350      评论:0      收藏:0      [点我收藏+]

while循环---->do--while循环

while语法:

  while(条件){

    //循环体

  }

特点:先判断、在执行

                                                while循环结构流程图

  技术分享

   do-while语法:

do{

//循环操作

}while(循环条件);    

特点:先执行 、在判断                               

 

                                             do-while循环流程图

技术分享

 

案例:

    int num=1;//初始变量

    while(num<=100){//循环条件

       System.out.println("好好学习,天天向上");//循环体

       num++;//改变迭代变量的值

    }

  注意点:所有的循环必须具备四要素

    01.初始变量

    02.循环条件

    03.循环体

    04.必须改变迭代变量的值

    

    合格了吗?

    String userInput="n";

    while(!userInput.equals("y")){

       //如果用户输入的不是y,那么执行循环体

       //上午阅读教材,下午编码

       合格了吗?

}

技术分享

public static void main(String[] args) {
        //
        double sheshidu = 0;
        int line = 1;       // 01.初始变量
        do {
            double huashidu = sheshidu * 9 / 5.0 + 32;
            //循环体
            System.out.println("摄氏度" + sheshidu + "\t华氏度" + huashidu);
            sheshidu += 20;
            line++;             //0.3改变迭代变量的值
        } while (line <= 10 && sheshidu <= 250);//02.循环条件
    }

}
    public static void main(String[] args) {
        // 
        int year=2012;//年份            01.初始变量
        double people=25;//
        while(people<=100){     //02.循环条件
            people= people*1.25;
            year++;    //0.3改变迭代变量的值
            System.out.println(year+"年\t培训人数达到了"+people);//循环体
            
        }
    }

}
public class T01 {

    public static void main(String[] args) {
        //
        System.out.print("请输入这是星期几:");
        Scanner input = new Scanner(System.in);
        // Ctrl+1 快速修正代码
        String weekday = input.next(); // 01.初始变量
        while (!weekday.equals("下周一")) {// 输入不是下周一 晚上写代码//02.循环条件

            System.out.println("every night wriet cote");
            System.out.print("请输入这是星期几:");
            weekday = input.next();// 0.3改变迭代变量的值
        }
    }

}

 

循环结构------>Java学习的第一个难点(while------do while)

原文:http://www.cnblogs.com/bocai2010000/p/6668124.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!