Pascal's triangle 2d array - formatting printed output

馋奶兔 提交于 2019-11-27 06:31:57

问题


I have a small assignment where I have to use a 2d array to produce Pascal's triangle. Here is my code, and it works. there is an extra credit opportunity if I display the triangle like so:

however, my spacing is not formatted like that. it simply displays the numbers all lined up on the left. its hard to describe but if you run it you will see what I mean.

here is my code:

import java.util.*;   public class Pascal {   public static final int ROW = 16;   public static void main(String[] args) {        int[][] pascal  = new int[ROW +1][];     pascal[1] = new int[1 + 2];     pascal[1][1] = 1;     for (int i = 2; i <= ROW; i++) {         pascal[i] = new int[i + 2];         for (int j = 1; j < pascal[i].length - 1; j++) {             pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];             }     }     for (int i = 1; i <= ROW; i++) {         for (int j = 1; j < pascal[i].length - 1; j++) {             System.out.print(pascal[i][j] + " ");         }         System.out.println();     } } } 

if someone could help me figure out how to add the correct spacing to my program to produce the output desired in the picture, that would be great :) I know I need to put a system out print " " somewhere I just dont know where. thanks!


回答1:


Here I had modified your code, it prints wonderfully for ROW size till 13, coz of the limitation of my console window.

import java.util.*;   public class Pascal {      public static final int ROW = 12;     private static int max = 0;      public static void main(String[] args) {          int[][] pascal  = new int[ROW +1][];         pascal[1] = new int[1 + 2];         pascal[1][1] = 1;         for (int i = 2; i <= ROW; i++) {             pascal[i] = new int[i + 2];             for (int j = 1; j < pascal[i].length - 1; j++) {                 pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];                 String str = Integer.toString(pascal[i][j]);                 int len = str.length();                 if (len > max)                     max = len;             }         }           for (int i = 1; i <= ROW; i++) {                             for (int k = ROW; k > i; k--)                 System.out.format("%-" + max + "s", " ");             for (int j = 1; j < pascal[i].length - 1; j++)                                       System.out.format("%-" + (max + max) + "s",  pascal[i][j]);             System.out.println();         }     } } 

Hopefully this might help.

Regards




回答2:


You're encountering spacing issues because you need to add whitespace to certain numbers to accommodate space that larger numbers occupy. First determine what the largest number you plan to print is (programmatically). Then determine the number of digits in that number log(n). You can then use this number to print whitespace for numbers that have less digits than your largest number to make your printing look nicer.



来源:https://stackoverflow.com/questions/8935254/pascals-triangle-2d-array-formatting-printed-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!