关于数位数的问题(求解)


import java.util.Scanner;

/*
* do…while循环:输入一个整数,判断它的位数
* 明明这个题目可以用String类型获取字符长度解决问题,为什么还要用循环(以提供代码)?
* 还有没有其他的解决方法
*The do... While loop: input an integer and determine its digits
 * why use loops when you can use String to get character lengths?
 * are there any other solutions?
* */
public class DoWhileText {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);


        /*
         * != 只要左边不等于右边  就是true
         * int 不会保留小数,只会保留整数部分
         * while();括号里面为true就继续执行 do {执行语句} 大括号里面的就执行
         * n 和 t 不能写入循环里面 外面会没法调用
         * */
        int n;
        int t = 0;
        int i;

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

        i = sc.nextInt();

        do {
            n = i / 10;
            t++;
            i = n;
            //System.out.println(t);
            //System.out.println(n);

        }while (n != 0);
        System.out.println("这是一个" + t + "位数");


        /*
        * String
        * */
        System.out.println("请输入你之前输入的数:");
        int de = sc.nextInt();

        String s = Integer.toString(de);
        int de1 = s.length();
        //System.out.println(s.length());
        System.out.println("这是一个" + de1 + "位数");
    }
}

结果:在这里插入图片描述

发布了18 篇原创文章 · 获赞 3 · 访问量 1071

猜你喜欢

转载自blog.csdn.net/hjh_cos/article/details/99664213