美文网首页零基础学Java语言
Java - 读正整数的位数

Java - 读正整数的位数

作者: 追风剑007 | 来源:发表于2017-12-26 10:32 被阅读0次

读入一个正整数,判断有多少位

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int n = 0;
        int count = 0;
        System.out.println("Enter a number:");
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        do {
        n = n / 10;
        count++;
        } while(n>0);
        if (count == 1) {
            System.out.println("The number consists of "+count+" digit.\n");
        }
        else{
            System.out.println("The number consists of "+count+" digits.\n");
        }
    }
}

输入35

Enter a number.
35
The number consists of 2 digits.

相关文章

网友评论

    本文标题:Java - 读正整数的位数

    本文链接:https://www.haomeiwen.com/subject/nxnhgxtx.html