编写程序数一下 1到 100 的所有整数中出现多少个数字9
class Test{
public static void main(String[] args) {
int count=0;
for(int i=0;i<100;i++){
if(i%10==9){
count++;
}
if(i/10==9){
count++;
}
}
System.out.println("count="+count);
}
}
截图
若将题目改为编写程序数一下 1到 100 的所有整数中多少个整数出现9。因为99中有两个9,只能算一次,可改为
class Test{
public static void main(String[] args) {
int count=0;
for(int i=0;i<100;i++){
if(i%10==9){
count++;
}else
if(i/10==9){
count++;
}
}
System.out.println("count="+count);
}
}
结果