打印等腰三角形_循环嵌套进阶应用


/* 用二重循环打印以下等腰三角形图形,注意空格、*号与行数的关系,5行示例图形如下:
* 4空格,1*号
*** 3空格,3*号
***** 2空格,5*号
******* 1空格,7*号
********* 0空格,9*号
*/

import java.util.Scanner;
public class Demo{
public static void main(String[] args){
//打印等腰三角形
Scanner input = new Scanner(System.in);
System.out.print("请输入三角形的行数:");
int row = input.nextInt();
for(int i=0; i for(int j=0; j System.out.print(" "); //输出空格,注意空格数量与行数的关系
}
for(int k=0; k<2*i+1; k++){
System.out.print("*"); //输出*号,注意*号数量与行数的关系
}
System.out.print("\n"); //每输出一行后换行
}
}
}
/*---------------------
请输入三角形的行数:13
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
-------------------------*/


下段代码是今天重习Java时写的,代码有些许不同,看来人的思维还不是固定的。——2014年3月21日 22:39:10

/*
*
***
*****
*******
*********
*/
import java.util.Scanner;
public class Test{
public static void main(String[] args){
System.out.print("请输入等腰三角形的行数:");
Scanner input = new Scanner(System.in);
int row = input.nextInt();
for(int i=1; i<=row; i++){
for(int j=i; j System.out.print(" ");
}
for(int k=1; k<=2*i-1; k++){
System.out.print("*");
}
System.out.println();
}

}

}
/*************************
请输入等腰三角形的行数:5
*
***
*****
*******
*********
*************************/

你可能感兴趣的:(Java)