4-15编程练习答案错误
if(num>=0&&num<=9999999999){
do
{
count++;
num/=10;
}while(num!=0);
System.out.println(count+"位数");
}else{
System.out.println("输入有误");
}
给出的答案会把0判断成0位数
if(num>=0&&num<=9999999999){
do
{
count++;
num/=10;
}while(num!=0);
System.out.println(count+"位数");
}else{
System.out.println("输入有误");
}
给出的答案会把0判断成0位数
2016-12-06
package study;
import java.util.Scanner;
public class NumWeiShu {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入一串小于10位的数字");
int num = in.nextInt();
int i = 0;
do{
num = num / 10;
i++;
if(num == 0){
System.out.println("您输入的数字是"+i+"位");
}
}while(num > 0);
}
}
public class HelloWorld { public static void main(String[] args) { int num = 0; int count =0; if(num>=0&&num<=999999) { do { count=count+1; num=num/10; }while(num!=0); System.out.println(count+"位数"); } else System.out.println("输入有误"); } }
试试这个代码,并没有太大的改变,只是把
count++;
num/= 10;
变成了count=count+1;
num=num/10;
举报