我有一个作业,我应该打印帕斯卡的三角形,到目前为止,我几乎已经解决了,但我的老师要求我们在中心显示三角形并以三角形形式显示。我尝试了很多东西,但是无法正确打印我的三角形,它总是打印在左边。我希望你能帮助我们,我会非常感谢你们。到目前为止,这是我的代码:
import javax.swing.*;
import java.util.*;
public class class1_030414_Rodrigo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String fila;
int f, a=0, b=0;
//
fila=JOptionPane.showInputDialog(null, "Until which line of the triangle would you like to prnt?");
f=Integer.parseInt(fila);
//
if (f<=-1){
JOptionPane.showMessageDialog (null, "You cannot type negative numbers.");
}
int triangulo [][] = new int[f][f];
//
for (a=0;a<f;a++){
for (b=0; b<f; b++){
triangulo[a][b] = 0;
}
}
for (a=0;a<f;a++){
triangulo[a][0] = 1;
}
for (a=1;a<f;a++) {
for (b=1;b<f;b++) {
triangulo[a][b] = triangulo[a-1][b-1] + triangulo[a-1][b];
}
}
for (a=0;a<f;a++){
for (b=0;a<f;a++){
}
}
for (a=0;a<f;a++) {
for(b=0;b<=a;b++) {
System.out.print( triangulo[a][b]);
}
System.out.println();
}
}