分析以下需求,并用代码实现:(封装成方法) (1)打印1到100之内的整数,但数字中包含m的要跳过 (2)每行输出n个满足条件的数,之间用空格分隔。

import java.util.Scanner;
class Home_Day05_01{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入想跳过包含几的数字和每行显示数字的个数");
        int m =  sc.nextInt();
        int n =  sc.nextInt();
        printNum(m,n);
    }

    /*
    分析以下需求,并用代码实现:(封装成方法)
    (1)打印1到100之内的整数,但数字中包含m的要跳过
    (2)每行输出n个满足条件的数,之间用空格分隔
    (3)如:
        1 2 3 4  5
        6 7 8 10 11
    返回值类型:void
    方法名称: printNum
    参数列表: int m 

    */
    public static void printNum(int m ,int n){
        int count = 0;
        for (int i = 1;i <= 100 ;i++ ){
            int ge = i %10;
            int shi = i /10 %10;
            if(!(ge == m || shi == m)){
                System.out.print(i+"  ");
                count++;
                if(count % n == 0){
                    System.out.println();
                }
            }
        }

    }

}

猜你喜欢

转载自www.cnblogs.com/xiuzidbk/p/9316000.html