pascal三角形正确格式化java
所以我现在正在做一项我似乎无法完成的任务。嗯,我已经把一切都做完了,但我想得到额外的学分。我一直在网上搜索,似乎找不到我想要的东西pascal三角形正确格式化java,java,formatting,pascals-triangle,Java,Formatting,Pascals Triangle,所以我现在正在做一项我似乎无法完成的任务。嗯,我已经把一切都做完了,但我想得到额外的学分。我一直在网上搜索,似乎找不到我想要的东西 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!");
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();
}
}
}
公共类PascalTester
{
公共静态void main(字符串[]args)
{
扫描仪kb=新扫描仪(System.in);
System.out.println(“欢迎使用Pascal的三角形程序!”);
System.out.println(“请输入所需三角形的大小”);
int size=kb.nextInt();
int[]myArray=新的int[size][size];
myArray=fillArray(myArray);
//myArray=calculateArray(myArray);
printArray(myArray);//打印数组
}
私有静态int[][]填充数组(int[][]数组)
{
数组[0][1]=1;
for(int i=1;i
我现在唯一的问题是正确地格式化输出,使其看起来像一个实际的三角形。此时此刻,任何建议都会非常有用。提前感谢尝试使用处的技术制作一个带有
array.length-i-1
空格的数组(需要在数字..和2个2位数之间添加数字空格,如果有的话..)
在外部for循环的开始处打印此数组。解决此问题的一种方法是,假设您将所有数字格式化为相同的宽度,将此问题视为线条居中的问题 Java编码留给读者作为练习,但本质上:
for lineText : triange lines
leadingSpacesCount = (80/2) - lineText.length();
print " " x leadingSpacesCount + lineText
这里的挑战是,您希望从三角形的顶部开始打印,但在到达三角形的最后一行(也是最宽的一行)之前,您不知道每行的居中位置。诀窍是在知道最后一行有多宽之前不要打印任何内容。一种方法是将所有行生成为
String
(或StringBuilder
)对象,并计算最大宽度。然后,从顶部开始,首先打印适当数量的空格,使每行居中。将显示正确的空格数
(maxLineLength - currentLine.length()) / 2
或者,您可以简单地假设一个最大线长度,并将所有线置于该宽度的中心。如果较长的线条超过最大宽度,则三角形将在某一行下方扭曲。(请确保不要尝试打印负数的空格!)如果有人正在寻找实际的代码来执行此操作,请查看我在Java中的实现,它与Craig Taylor提到的类似(数字格式化为相同的宽度),而且它使用算法来计算没有内存的元素(或阶乘) 代码中有注释,解释每个步骤(计算和打印):
/**
*此方法将打印Pascal三角形的第一级。信息技术
*使用中描述的方法:
*
* https://en.wikipedia.org/wiki/Pascal%27s_triangle#Calculating_a_row_or_diagonal_by_itself
*
*它基本上计算当前行和列的组合
*乘以上一个(开始时始终为1
*每个pascal三角形行的)。它将把每个树元素打印到输出中
*流,将数字与空格对齐,形成一个完美的三角形。
*
*@param num
*#要打印的级别
*/
公共静态无效printPascalTriangle(int num){
//创建一个便笺簿(#个空格)在数字之间显示以保存内容
//按顺序。此值应大于最高值的#位数
//应为奇数,且应为奇数(具有相同的
//数字之间左侧和右侧的空格数)
int pad=7;
//计算每个数字左边的#个空格加上自身
//(这是三角形台阶的宽度)
int stepsWidth=pad/2+1;
//现在计算从左侧开始的最大空间
//屏幕显示到第一个三角形的级别(我们将在
//三角形)
整数空间=(num-1)*步长;
对于(int n=0;n0){
System.out.printf(“%”+空格+s“,”);
空间-=阶梯宽度;
}
//这将代表先前的组合C(n k-1)
int-prevCombination=1;
对于(int k=1;k当前输出是什么?你希望它看起来像什么?你尝试过什么?这是一个直角三角形atm,我想做一个等腰三角形。我尝试过弄乱printf格式,但当我弄错时,我在编译器中得到错误。你应该展示你尝试过的东西,毕竟,如果我们这样做,你值得额外的学分吗?我已经做到了我一直在尝试不同的printf资料,看看我是否能把它放到tab上,然后首先要做的是用你想要的格式手工写出来,然后仔细考虑你到底做了什么。即使链接可能包含答案,也请在这里发布你的相关代码。很好,打电话给bish。代码是发布的,而不是链接。
/**
* This method will print the first # levels of the Pascal's triangle. It
* uses the method described in:
*
* https://en.wikipedia.org/wiki/Pascal%27s_triangle#Calculating_a_row_or_diagonal_by_itself
*
* It basically computes the Combinations of the current row and col
* multiplied by the previous one (which will always be 1 at the beginning
* of each pascal triangle row). It will print each tree element to the output
* stream, aligning the numbers with spaces to form a perfect triangle.
*
* @param num
* # of levels to print
*/
public static void printPascalTriangle(int num) {
// Create a pad (# of spaces) to display between numbers to keep things
// in order. This should be bigger than the # of digits of the highest
// expected number and it should be an odd number (to have the same
// number of spaces to the left and to the right between numbers)
int pad = 7;
// Calculate the # of spaces to the left of each number plus itself
// (this is the width of the steps of the triangle)
int stepsWidth = pad / 2 + 1;
// Now calculate the maximum # of spaces from the left side of the
// screen to the first triangle's level (we will have num-1 steps in the
// triangle)
int spaces = (num - 1) * stepsWidth;
for (int n = 0; n < num; n++) {
// Print the left spaces of the current level, deduct the size of a
// number in each row
if (spaces > 0) {
System.out.printf("%" + spaces + "s", "");
spaces -= stepsWidth;
}
// This will represent the previous combination C(n k-1)
int prevCombination = 1;
for (int k = 1; k <= n + 1; k++) {
System.out.print(prevCombination);
// Calculate how many digits this number has and deduct that to
// the pad between numbers to keep everything aligned
int digits = (int) Math.log10(prevCombination);
if (digits < pad) {
System.out.printf("%" + (pad - digits) + "s", "");
}
// Formula from Wikipedia (we can remove that "+1" if we start
// the row loop at n=1)
prevCombination = prevCombination * (n + 1 - k) / k;
}
// Row separator
System.out.println();
}
}