题目描述:逢7过指如果一个数是7的倍数或含有数字7,则该数为逢7过数。输入两个整数,求出在这范围内所有逢7过的数。
题目分析:首先考察一定范围,比如1~100的区间。
public class pass7 {
public static void main(String[] args) {
for(int i = 1 ; i <= 100 ; i++) {
if (i % 7 == 0 || i % 10 == 7 || (i / 10) % 10 == 7) {
System.out.println(i);
}else {
continue;
}
}
}
}
后面考察输入任一整数,判断该数是否为逢7过数。
import java.util.Scanner;
public class newpass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
int sum = i;
int a = 1;
System.out.println("Find wether or not " + i + " is a 7-related number.");
while(sum != 0) {
if(sum % 10 == 7) {
a = 0;
break; /*判断每一个数位是否含7,如含7,用a=0记录并结束循环*/
}
sum = sum / 10;
}
if (i % 7 == 0 || a == 0) {
System.out.println("Yes, it is!");
} else {
System.out.println("No, it isn't!");
}
}
}
结果
最后综合以上两种简化情况,得到任意范围内打印逢7过数字
import java.util.Scanner;
public class newpass7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter start and end number to find 7-related ones between them.");
int start = sc.nextInt();
int end = sc.nextInt();
int sum;
int a;
for (int i = start ; i <= end ; i++ ) {
sum = i;
a = 1;
while (sum != 0) {
if (sum % 10 == 7) {
a = 0;
break; /*将任一整数的寻找方法放入大循环即可*/
}
sum = sum / 10;
}
if (i % 7 == 0 || a == 0) {
System.out.println(i);
}
}
}
}
结果: