逢7过问题解决

题目描述:逢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);
			}
		}
	}
}

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值