Java编写四边形
1,正方形
private static void fun1() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要打的边长数:");
int a = sc.nextInt();
for (int i = 0; i < a; i++) { //打印正方形的高
for (int j = 0; j < a; j++) { //打印正方形的长
System.out.print("*"+" ");
}
System.out.println(" ");
}
}
2,平行四边形
//平行四边形
private static void fun2() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要打的边长数:");
int a = sc.nextInt();
for (int i = 1; i <= a; i++) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= a; j++) {
System.out.print("*"+" ");
}
System.out.println(" ");
}
}
3,打印三角形
private static void fun3() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要打的边长数:");
int a = sc.nextInt();
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= a-i; j++) {
System.out.print(" ");
}
for (int j = 1; j < i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
4,打印菱形
private static void fun4() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要打的边长数:");
int a = sc.nextInt();
//上部分
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= a-i; j++) {
System.out.print(" ");
}
for (int j = 1; j < i; j++) {
System.out.print("* ");
}
System.out.println();
}
//下部分
for (int i = 1; i <= a-1; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(" ");
}
for (int j = 1; j <a-i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
5,打印空心菱形
//空心菱形
private static void fun5() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要打的边长数:");
int a = sc.nextInt();
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= a-i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
if (j==1||j==i){
System.out.print("* ");
}else {
System.out.print(" ");
}
}
System.out.println();
}
for (int i = 1; i <= a-1; i++) {
for (int j = 1; j <=i; j++) {
System.out.print(" ");
}
for (int j = 1; j <=a-i; j++) {
if (j==1||j==a-i){
System.out.print("* ");
}else {
System.out.print(" ");
}
}
System.out.println();
}
}