Java中的数字三角形

时间:2015-03-09 12:50:51

标签: java numbers modulo

我正在尝试编写一个打印出由数字组成的三角形的程序。看起来应该是这样的:

           1
         2 3 4
       3 4 5 6 7
     4 5 6 7 8 9 0
   5 6 7 8 9 0 1 2 3
 6 7 8 9 0 1 2 3 4 5 6    

在我的情况下,它返回负数(876543210-1-2-3 ...),但它应该只使用0-9。我可以使用modulo n%10,但我不知道怎么写。有帮助吗?谢谢。

import java.util.Scanner ;

public class Triangle {
public static void main (String [] args) {
    Scanner sc = new Scanner(System.in);

    System.out.print("Number: ");
    int n = sc.nextInt();
    int j;
    int i;
    int k = n-1;

    System.out.printf("n=%d\n\n", n);

    for (i=1; i<=(n*2); i=i+2) {
        for (j=0; j<=2*n-1; j++) {
            if (j < k){
                System.out.print(" ");
            }
            else if (j < (k+i)){ 
                    System.out.printf("%d", (n-j));
            }
            else {
                System.out.print(" ");              
            }       
        }
        k = k-1;
    System.out.println();
    }
}
}

2 个答案:

答案 0 :(得分:0)

这是一个可能的实现,我修改了你的索引名称,因为它有点令人困惑:

public class Triangle {
    public static void main (String [] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Number: ");
        int n = sc.nextInt();

        int startNumber = 1; /* First number in the line */
        int spaces = n - 1;  /* Spaces in the current line */
        int numbers = 1; /* Numbers in the current line */

        System.out.printf("n=%d\n\n", n);


        for (int lineCounter = 0; lineCounter < n; lineCounter++) {
            /* Spaces before the numbers */
            for (int spaceCounter = 0; spaceCounter < spaces; spaceCounter++) {
                System.out.print(" ");
            }
            for (int numberCounter = 0, number = startNumber; numberCounter < numbers; numberCounter++) {
                System.out.printf("%d", (number));
                number = (number + 1) % 10;
            }
            /* Spaces after the numbers */
            for (int spaceCounter = 0; spaceCounter < spaces; spaceCounter++) {
                System.out.print(" ");
            }
            System.out.println();
            startNumber = (startNumber + 1) % 10;
            spaces--;
            numbers += 2;
        }
    }
}

答案 1 :(得分:0)

您可以像这样创建printTriangle()方法(注释中的说明如下):

static void printTriangle(int numLines) {
    for (int lineNumber = 1; lineNumber <= numLines; lineNumber++) {
        // Print 2 * (numLines - lineNumber) spaces before the first number in the current line
        for (int spacesPerLine = 0; spacesPerLine < 2 * (numLines - lineNumber); spacesPerLine++) {
            System.out.print(" ");
        }
        // First line has 1 number, second has 3, third has 5, etc.
        int numbersPerLine = (2 * lineNumber) - 1;
        // Print the numbers in the current line
        // from lineNumber (inclusive) to lineNumber + numbersPerLine (exclusive)
        for (int number = lineNumber; number < lineNumber + numbersPerLine; number++) {
            System.out.print((number % 10) + " ");
        }
        System.out.println();
    }
}