在Java中打印方形图案 - 关闭一个错误

时间:2016-04-20 18:14:32

标签: java loops

我会打印以下图案,但我没有得到第一列和第一行,我真的不明白。也许一个错误,但如何纠正它?谢谢你的解释。

预期产出:

00 10 20 30 40 50 60 70 80 90
01 11 21 31 41 51 61 71 81 91
02 12 22 32 42 52 62 72 82 92
03 13 23 33 43 53 63 73 83 93
04 14 24 34 44 54 64 74 84 94
05 15 25 35 45 55 65 75 85 95
06 16 26 36 46 56 66 76 86 96
07 17 27 37 47 57 67 77 87 97
08 18 28 38 48 58 68 78 88 98
09 19 29 39 49 59 69 79 89 99 

代码:

public static void main (String args[]) {  

    for(int row=0;row<9;row++){
        int x;
        x=row;
        x++;

        for(int col=0;col<9;col++){
           x = x+10;   
           StringBuilder mySB = new StringBuilder(x+" ");
           System.out.print(mySB);   
        }

        System.out.println();

    }
}

4 个答案:

答案 0 :(得分:3)

您需要在打印后放置x = x + 10,然后移除x++;行。

您还需要转到<=9<10,因为您错过了9:

public static void main (String args[]) {  

    for(int row=0;row<10;row++){
        int x;
        x=row;

        for(int col=0;col<10;col++){ 
           StringBuilder mySB = new StringBuilder(x+" ");
           System.out.print(mySB);   
           x = x+10;  
        }

        System.out.println();

    }
}

修改  Nifty Java 8的做法:

private static class Grid {

    public static void outputGrid() {
        IntStream
            .range(0, 10)
            .mapToObj(Grid::generateRow)
            .forEach(System.out::println);
    }

    private static String generateRow(int row) {
        return IntStream
            .range(0, 10)
            .mapToObj(col -> String.format("%d%d", col, row))
            .collect(Collectors.joining(" "));
    }
}

public static void main(String []args){
   Grid.outputGrid();
}

答案 1 :(得分:2)

这样怎么样:

for(int x = 0; x < 10; x++) {
    for(int y = 0; y < 10; y++) {
        System.out.print(Integer.toString(y) + Integer.toString(x) + " ");
    }
    System.out.println();
}

输出:

00 10 20 30 40 50 60 70 80 90 
01 11 21 31 41 51 61 71 81 91 
02 12 22 32 42 52 62 72 82 92 
03 13 23 33 43 53 63 73 83 93 
04 14 24 34 44 54 64 74 84 94 
05 15 25 35 45 55 65 75 85 95 
06 16 26 36 46 56 66 76 86 96 
07 17 27 37 47 57 67 77 87 97 
08 18 28 38 48 58 68 78 88 98 
09 19 29 39 49 59 69 79 89 99 

答案 2 :(得分:1)

public class Stack1
{

public static void main (String args[]) {  

    StringBuilder mySB = new StringBuilder() ;
    //You need to print 10x10 matrix 
    for(int row=0 ; row<10 ; row++){
        int x;
        x=row;

        for(int col=0;col<10;col++){
           if (x < 10)  //for first row
               mySB = new StringBuilder(0+""+x+" ") ;
           //for other rows
           else
               mySB = new StringBuilder(x+" ") ;
           System.out.print(mySB);   
           x = x+10;  
        }

        System.out.println();

    }
}

}

输出:

00 10 20 30 40 50 60 70 80 90
01 11 21 31 41 51 61 71 81 91
02 12 22 32 42 52 62 72 82 92
03 13 23 33 43 53 63 73 83 93
04 14 24 34 44 54 64 74 84 94
05 15 25 35 45 55 65 75 85 95
06 16 26 36 46 56 66 76 86 96
07 17 27 37 47 57 67 77 87 97
08 18 28 38 48 58 68 78 88 98
09 19 29 39 49 59 69 79 89 99

答案 3 :(得分:0)

解决方案:

IntStream.rangeClosed(0, 9)
                .forEach(i -> IntStream.rangeClosed(0, 9)
                        .mapToObj(j -> j == 9 ? Integer.toString(j) + Integer.toString(i) + "\n" : Integer.toString(j) + Integer.toString(i) + "  ")
                        .forEach(System.out::print)
                );

输出:

00  10  20  30  40  50  60  70  80  90
01  11  21  31  41  51  61  71  81  91
02  12  22  32  42  52  62  72  82  92
03  13  23  33  43  53  63  73  83  93
04  14  24  34  44  54  64  74  84  94
05  15  25  35  45  55  65  75  85  95
06  16  26  36  46  56  66  76  86  96
07  17  27  37  47  57  67  77  87  97
08  18  28  38  48  58  68  78  88  98
09  19  29  39  49  59  69  79  89  99