Java 循环结构 while

    技术2022-07-10  158

    循环结构的分类:for、while、do…while语句

    while语句格式:

    初始化语句; while(判断条件语句){ 循环语句; 控制条件语句; }

    执行流程:

    执行初始化语句执行判断条件语句,看其返回值是ture还是false 如果是true,就继续执行如果是false,就结束循环 执行循环体语句继续判断条件语句,执行上面操作

    1、基础案例:

    int x = 1; while(x <= 10){ System.out.println("x = " + x); x++; }

    2、使用while来统计水仙花数共有多少:

    所谓水仙花数是指一个三位数,其各位数字的立方和等于该数本身。 举例:153 = 111 + 555 + 333 = 153

    int count = 0; int i = 100; while(i <= 999){ int ge = i % 10; int shi = i / 10 % 10; int bai = i / 100; if( i == ge * ge *ge + shi *shi * shi + bai * bai * bai){ count ++; } i++; } System.out.println(count);
    Processed: 0.009, SQL: 9