zoukankan      html  css  js  c++  java
  • Java知识系统回顾整理01基础05控制流程03 while

    while和do-while循环语句

    一、while:条件为true时 重复执行

    只要while中的表达式成立,就会不断地循环执行

       

    public class HelloWorld {

        public static void main(String[] args) {       

            //打印0到4    

            int i = 0;

            while(i<5){

                System.out.println(i);

                i++;

            }

        }

    }

       

    二、do-while :条件为true时 重复执行,至少会执行一次

    do{

    } while 循环

    与while的区别是,无论是否成立,先执行一次,再进行判断

    public class HelloWorld {

        public static void main(String[] args) {

              

            //打印0到4

            //与while的区别是,无论是否成立,先执行一次,再进行判断

            int i = 0;

            do{

                System.out.println(i);

                i++;          

            } while(i<5);

              

        }

    }

       

    三、练习--阶乘

    题目:

    通过Scanner获取一个整数,然后使用while计算这个整数的阶乘

    N的阶乘等于 N* (N-1) * (N-2) * ... * 1

    要求效果:

       

    官方答案:

    import java.util.Scanner;

        

    public class HelloWorld {

        public static void main(String[] args) {

            Scanner s = new Scanner(System.in);

            System.out.println("请输入一个整数:");

            int n = s.nextInt();

            int fac = 1;

            while(n>=1){

                fac *=n;

                n--;

            }

            System.out.println("阶乘是:" + fac);

        }

    }

       

       

       

  • 相关阅读:
    flask-script插件
    狗书(flask基础)
    2018.1.18纪事
    py3.6 + xadmin的自学网站搭建
    使用selenium抓取淘宝的商品信息
    pyquery操作
    requests模块
    python3里的Urllib库
    随便写点
    How many ways?? HDU
  • 原文地址:https://www.cnblogs.com/xlfcjx/p/10770602.html
Copyright © 2011-2022 走看看