| 12345678910111213141516171819202122232425 |
- /**
- * @author WanJl
- * @version 1.0
- * @title Demo04
- * @description 课堂练习: 给定整数 12345,使用 while 循环统计它有多少位并输出。
- * @create 2026/7/15
- */
- public class Demo04 {
- /*
- for 一般来讲,是知道要循环多少次,所以使用for循环
- for(int i=0;i<100;i++)
- while 一般来讲,是不知道要循环多少次,所以使用while循环
- 比如 给定一个数,一直除以2,直到等于0为止,不知道要除多少次。
- while(a!=0)
- */
- public static void main(String[] args) {
- int n=12345;
- int t=0; //统计除的个数,也就是位数,每次除以10,统计1位
- while (n!=0){
- n/=10; //每次都除以10,缩小10倍
- t++; //每次缩小10,累计1
- }
- System.out.println(t);
- }
- }
|