相信很多人在不知道答案的情况下一定会认为答案是10次或者是11次,其实并不是!我们来看答案:
错误示例:
public class homework1 {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <=100 ; i++) {
if (i%10==9||i/10==9){
count++;
}
}
System.out.println(count);
}
}
测试结果:
19
正确示例:
public class TestDemo3 {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 100; i++) {
if(i / 10 == 9) {
count++;
}
if(i % 10 == 9) {
count++;
}
}
System.out.println(count);
}
}
测试结果:
20
1-100数字中出现过9的数字如下(99中出现过9两次):
9 19 29 39 49 59 69 79 89
90 91 92 93 94 95 96 97 98 99



