Java中的偶数三角形

时间:2018-10-20 17:57:12

标签: java

我该如何制作相同的三角形,但是要有偶数和空格?我该如何生成没有任何限制的偶数个数,并适应用户输入的行数(用户仅输入行数,而没有限制生成偶数或奇数数)

int num = 5;
int cont = 0;
for (int i = 1; i <= num; i ++) {
cont + = 2;
    for (int j = 0; j <num-i; j ++) {
    System.out.print ("");
}
    for (int k = i; k <cont; k ++) {
        System.out.print (k);
    }
    for (int l = i; l <cont-1; l ++) {
        System.out.print (l);
    }

System.out.println ("");
}

该程序的运行是:

    1
   232
  34543
 4567654
567898765

我想要什么,但是他怎么做?

     2
    4 6
  8 10 12
14 16 18 20

4 个答案:

答案 0 :(得分:0)

如果我正确理解它,那很容易,但是数字5之后,数字会彼此重叠,因此,如果要避免这种情况,必须为每个打印数字使用2或3个空格。可以使用以下方法创建主要三角形:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int row = in.nextInt();
    int allSpace = 2 * row - 1;
    for (int i = 1; i <= row; i++) {
        int allNumber = 2 * i - 1;
        for (int j = 0; j < (allSpace - allNumber) / 2; j++) {
            System.out.print("   ");
        }
        for (int j = 1; j <= allNumber ; j++) {
            System.out.printf("%3d", j);
        }
        for (int j = allNumber - 1; j >= i ; j--) {
            System.out.printf("%3d", j);
        }
        System.out.println();
    }
}

但是对于偶数,您可以执行以下操作:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int row = in.nextInt();
    int allSpace = 2 * row - 1;
    int even = 2;
    for (int i = 1; i <= row; i++) {
        int allNumber = 2 * i - 1;
        for (int j = 0; j < (allSpace - allNumber) / 2; j++) {
            System.out.print("   ");
        }
        for (int j = 1; j <= allNumber ; j++) {
            System.out.printf("%3d", even);
            even += 2;
        }
        System.out.println();
    }
}

我正确理解了吗?

答案 1 :(得分:0)

这有点棘手,因为位数会稍微影响对齐方式。

这将起作用:

    int num = 5;
    int cont = 0;
    for (int i = 1; i <= num; i++) {
        for(int k = 1; k <= num -i; k++) System.out.print(" ");
        for(int j = 1; j <= i; j++) {
            cont += 2;
            System.out.print(cont + " ");
        }

    System.out.println ("");
    }

答案 2 :(得分:0)

这将为您带来想要的结果

    int rows = 500;

    //Calculating the largest number we will reach:
    int largest = 1;
    for(int i=1;i<=rows;i++){  

        largest=largest + i;    
    } 
    largest=largest*2;//since we only count even numbers


    int cur = 2; //current value to add to the triangle
    int rowsize = 0; //how many values to fit inside the row

    for (int i = 1; i <= rows; i ++) {

        int cursize = 0; //ammount of values currently in the row

        //filling spaces on left side
        //The value is based on how many rows we want, more rows = need for more initial spacing
        String spaces = "";
        for(int j = 0 ; j < (rows + "").length() ; j++) {
            spaces += " ";
        }
        for (int j = 0; j <= rows - i ; j++) {

            System.out.print(spaces);
        }

        while(cursize <= rowsize) {

            //For each number, we calculate how much spacing to place before it
            int len = (largest + "").length() - (cur + "").length();
            spaces = "";
            while(len != 0) {
                spaces += " ";
                len--;
            }

            System.out.print(" " + spaces + cur);
            cur += 2;
            cursize++;

        }

        System.out.println (""); //new line
        rowsize++;
    }
}

更新:该代码现在可以管理间距并最多处理675行

答案 3 :(得分:0)

int number=2;
for(int row=1;row<=4;row++) {
    for(int space=1;space<=(4-row);space++) {
        System.out.print(" ");
    }
    for(int noOfValue=1;noOfValue<=row;noOfValue++) {
        System.out.print(number+" ");
        number=number+2;
    }
    System.out.println();
}

  • row->表示它具有的行数。(有4行)
  • space->表示第一个字符开始前每行中的空格数。
  • noOfValue->它表示每行必须打印多少个值。
  • number->这是将要打印的实际值。