黑马程序员技术交流社区

标题: 打印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
  1. import java.util.Scanner;

  2. class Test3 {

  3.        
  4.         public static int shuZi9(int a) {                             //判断一个数每一位是否包含9

  5.                 while(true) {
  6.                
  7.                         int b = a%10;
  8.                         a /=10;
  9.                
  10.                         if (b==9) {
  11.                        
  12.                                 int c = 1;
  13.                                 return c;                        //return直接结束了方法,也就跳出了死循环
  14.                        
  15.                         }
  16.                
  17.                         if (a==0) {
  18.                                 int c =0;
  19.                                 return c;
  20.                         }
  21.                        
  22.                 }
  23.         }
  24.        
  25.         public static void main(String[] args) {
  26.        
  27.                 Scanner sc = new Scanner(System.in);
  28.        
  29.                 System.out.print("请输入要检测的数据:");
  30.        
  31.                 int a = sc.nextInt();

  32.                 for (int x = 1;x<=a ;x++ ) {
  33.                
  34.                         int b = shuZi9(x);

  35.                         if(b==1) {
  36.                        
  37.                                 continue;
  38.                         }
  39.                
  40.                         System.out.println(x);
  41.                 }
  42.                
  43. }
  44. }
复制代码
试着改了一下,随意键入一个数字,判断每一位是否有9,有则跳出一次。






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2