Java教程

Java打印图案8

用于在控制台上打印以下模式的Java程序

0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321

程序:

public class pattern{
    public static void main(String []args){
        int lines=10;
        int i,j;
        int count;
        for(i=1;i<=lines;i++){
            // this loop is used to print lines
count=0;
            for(j=1;j<lines;j++){
                // this loop is used to print numbers in a line
             if(count<i-1){
                    if(!(j<lines-i+1)){
                        System.out.print(j);
                        count++;
                    }
                }
            }
            System.out.print("0");
            count=0;
            for(--j;j>=1;j--){
                // this loop is used to print j and increment count
             if(count<i-1){
                    System.out.print(j);
                    count++;
                }
            }
            System.out.println("");
        }
    }
}
输出:
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4