码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-10-29 00:30:35      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:java   编程   

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编程:用两种方法求输入正整数的位数。

标签:java   编程   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!