慕哥5975565
2018-05-11 22:51
/*
* 判断一个数(小于10位)的位数。 输入999,则输出 “它是个3位的数!”
*/
int num = 999;
int count = 0;
if (num != 0 && num <= 999999999) {
do {
count++;
num = num / 10;
}
while{
(num<10);
}
System.out.println("它是个" + count + "位的数!");
} else{
System.out.println("输入有误");
}
}
}
public class HelloWorld{
public static void main(String[] args){
int num = 999;
int count = 0;
if (num >= 0 && num <= 999999999) {
do {
count++;
num /= 10;
}while(num>=1);
System.out.println("它是个" + count + "位的数!");
} else{
System.out.println("输入有误");
}
}
}
do while有bug吧
public static void main(String[] args){
Scanner sr=new Scanner(System.in);
int num =sr.nextInt();
int count = 0;
if (num > 0 && num <= 1000000) {
while(num!=0) {
count++;
num/=10;
}
System.out.println("它是"+count+"位数");
}else {
System.out.println("输出错误!");
}
}
}
Scanner sr=new Scanner(System.in);
int num =sr.nextInt();
int count = 0;
if (num != 0 && num <= 999999999) {
do {
count++;
num/=10;
}while(num>1);
System.out.println("它是"+count+"位数");
}else {
System.out.println("输出错误!");
}
}
为啥输入数字会减少一位数
public class HelloWorld{
public static void main(String[] args){
int num = 999;
int count = 0;
do
{
count++;
num=num/10;
}
while(num >0&&num<=999999999);
System.out.println("它是个" + count + "位的数!");
}
}
Java入门第一季(IDEA工具)升级版
1165639 学习 · 17587 问题
相似问题