首页 > 代码库 > Java编程:用两种方法求输入正整数的位数。
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编程:用两种方法求输入正整数的位数。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。