Java语言怎么使用循环语句和if语句配合的办法,打印*号三角形
for(int i=1;i<4;i++) {
for(int j=1;j<4-i;j++) {
System.out.print(" ");}
for(int j=1;j<=2*i-1;j++){
System.out.print("*");
}
System.out.println();
}
主要是看星号,空格和循环的关系然后根据关系写循环
eg: i 空格 *
1 2 1
2 1 3
3 0 5
空格=i的最大值-i
*=2*i-1
int n = 5; //等腰三角形层数
for(int i = 1; i <= n; i++) {
for(int j = n-i; j > 0; j--) {
System.out.print(" ");
}
for(int k = 1; k <= 2*i-1; k++) {
System.out.print("*");
}
System.out.println();
}
Scanner sc = new Scanner(System.in );
System.out.println("请输入行数");
int rows = sc.nextInt();
for (int row = 1;row<=rows;row++) {
for (int col = 1; col <=rows-row; col++) { //行数的空格是:行数-行号
System.out.print(" ");
}
for (int col = 1; col <= row*2-1; col++) {
System.out.print("*");
}
System.out.println();
}