黑马程序员技术交流社区
标题:
打印1到100之内的整数,但数字中包含9的要跳过
[打印本页]
作者:
vvvvvc
时间:
2016-6-2 01:26
标题:
打印1到100之内的整数,但数字中包含9的要跳过
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 100; i++) {
if (i % 10 == 9 || i / 10 % 10 == 9) {
continue;
}
System.out.print(i + " ");
count++;
if (count % 5 == 0) {
System.out.println();
}
// count++;
}
}
作者:
黑猫的消失
时间:
2016-6-2 20:24
import java.util.Scanner;
class Test3 {
public static int shuZi9(int a) { //判断一个数每一位是否包含9
while(true) {
int b = a%10;
a /=10;
if (b==9) {
int c = 1;
return c; //return直接结束了方法,也就跳出了死循环
}
if (a==0) {
int c =0;
return c;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入要检测的数据:");
int a = sc.nextInt();
for (int x = 1;x<=a ;x++ ) {
int b = shuZi9(x);
if(b==1) {
continue;
}
System.out.println(x);
}
}
}
复制代码
试着改了一下,随意键入一个数字,判断每一位是否有9,有则跳出一次。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2