首页 > 编程语言 > 详细

Java编程:用两种方法求输入正整数的位数。

时间:2014-10-29 00:28:28      阅读:287      评论:0      收藏:0      [点我收藏+]
import java.util.Scanner;

public class Test {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		//把整数n转换为字符串求其长度
		int len = Integer.toString(n).length();
		System.out.println("用字符串的方式求其长度len="+len);
		
		//用while循环求其长度
		int count = 0;
		while(true)
		{
			count++;
			n = n/10;
			if(n==0)
				break;
		}
		System.out.println("用while循环求其字符串的长度len"+count);
		
	}

}

Java编程:用两种方法求输入正整数的位数。

原文:http://blog.csdn.net/u012110719/article/details/40556295

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!