【发布时间】:2013-01-17 03:27:25
【问题描述】:
所以我目前正在处理一项我似乎无法完成的任务。好吧,我已经完成了一切,但想要额外的功劳。我一直在网上四处寻找,但似乎无法找到我正在寻找的确切内容。
public class PascalTester
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.println("Welcome to the Pascal's Triangle program!");
System.out.println("Please enter the size of the triangle you want");
int size = kb.nextInt();
int[][] myArray = new int[size][size];
myArray = fillArray(myArray);
//myArray = calculateArray(myArray);
printArray(myArray); //prints the array
}
private static int[][] fillArray(int[][] array)
{
array[0][1] = 1;
for (int i = 1; i < array.length; i++)
{
for (int j = 1; j < array[i].length; j++)
{
array[i][j] = array[i-1][j-1] + array[i-1][j];
}
}
return array;
}
private static void printArray(int[][] array)
{
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
if(array[i][j] != 0)
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
我现在唯一遇到的问题是正确格式化输出,使其看起来像一个实际的三角形。此时任何建议都会非常有帮助。提前致谢
【问题讨论】:
-
当前输出是多少?你希望它看起来像什么?你试过什么?
-
这是一个直角三角形 atm,我正在寻找一个等腰三角形。我试过弄乱 printf 格式,但是当我这样做时,我在编译器中遇到错误
-
你应该展示你尝试过的东西,毕竟,如果我们这样做了,你应该得到额外的荣誉吗?
-
我一直在尝试不同的 printf 东西,看看我能不能把它变成标签和东西
-
首先要做的是以你想要的格式手写出来,然后仔细考虑你到底做了什么。
标签: java formatting pascals-triangle