JAVA学习之旅--打印图形

时间:2022-01-18 11:10:02

题目如下:



程序如下:

package Day03.homework;


import java.util.Scanner;


public class PrintGrap {


public static void main(String[] args) {
System.out.println("===========打印图形===========");
while (true) {
System.out.println("请输入需要打印的图形序号:");
System.out.println("1.空心矩形");
System.out.println("2.实心矩形");
System.out.println("3.实心上三角形右");
System.out.println("4.实心上三角形左");
System.out.println("5.实心下三角形右");
System.out.println("6.实心下三角形左");
System.out.println("7.空心上三角形右");
System.out.println("8.空心上三角形左");
System.out.println("9.空心下三角形右");
System.out.println("10.空心下三角形左");
System.out.println("11.空心菱形");
System.out.println("12.实心菱形");
Scanner sc = new Scanner(System.in);
int sel = sc.nextInt();
switch (sel) {
case 1:
one("空心矩形:");
break;
case 2:
two("实心矩形:");
break;
case 3:
the("实心上三角形右:");
break;
case 4:
fou("实心上三角形左:");
break;
case 5:
fiv("实心下三角形右:");
break;
case 6:
six("实心下三角形左:");
break;
case 7:
sev("空心上三角形右:");
break;
case 8:
eig("空心上三角形左:");


break;
case 9:
nin("空心下三角形右:");
break;
case 10:
ten("空心下三角形左:");
break;
case 11:
ele("空心菱形:");
break;
case 12:
twe("实心菱形:");
break;
case 0:
System.out.println("欢迎下次使用,程序结束!!!");
System.exit(0);
default:
System.out.println("非法输入!!!");
break;
}
}
}


public static void one(String str) {
System.out.println(str);
for (int i = 0; i <= 5; i++) {
for (int j = 0; j <= 11; j++) {
if (j == 0 || j == 11 || i == 0 || i == 5) {
System.out.print("#");
} else {
System.out.print(" ");
}
}
System.out.println();
}
System.out.println("===========================");
}


public static void two(String str) {
System.out.println(str);
for (int i = 0; i <= 5; i++) {
for (int j = 0; j <= 11; j++) {
System.out.print("#");
}
System.out.println();
}
System.out.println("===========================");
}


public static void the(String str) {
System.out.println(str);
for (int i = 0; i < 10; i++) {
for (int l = 0; l < i; l++) {
System.out.print(" ");
}
for (int j = 0; j < 10 - i; j++) {
System.out.print("#");
}
System.out.println();
}
System.out.println("===========================");
}


public static void fou(String str) {
System.out.println(str);
for (int i = 10; i >= 1; i--) {
for (int j = 10; j > (10 - i); j--) {
System.out.print("#");
}
System.out.println();
}
System.out.println("===========================");
}


public static void fiv(String str) {
System.out.println(str);
for (int i = 1; i <= 10; i++) {
for (int l = 1; l <= 10 - i; l++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {


System.out.print("#");
}
System.out.println();
}
System.out.println("===========================");
}


public static void six(String str) {
System.out.println(str);
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= i; j++) {


System.out.print("#");
}
System.out.println();
}
System.out.println("===========================");
}


public static void sev(String str) {
System.out.println(str);
for (int i = 0; i < 10; i++) {
for (int l = 0; l < i; l++) {
System.out.print(" ");
}
for (int j = 0; j < 10 - i; j++) {
if (i == 0 || i >= 9) {
System.out.print("#");
} else {
if (j == 9 - i || j == 0) {
System.out.print("#");
} else {
System.out.print(" ");
}
}
}
System.out.println();
}
System.out.println("===========================");
}


public static void eig(String str) {
System.out.println(str);
for (int i = 10; i >= 1; i--) {
for (int j = 10; j > (10 - i); j--) {
if (i == 10 || i <= 2) {
System.out.print("#");
} else {
if (j == 11 - i || j == 10) {
System.out.print("#");
} else {
System.out.print(" ");
}
}
}
System.out.println();
}
System.out.println("===========================");
}


public static void nin(String str) {
System.out.println(str);
for (int i = 1; i <= 10; i++) {
for (int l = 1; l <= 10 - i; l++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
if (i == 10 || i <= 2) {
System.out.print("#");
} else {
if (j == 1 || j == i) {
System.out.print("#");
} else {
System.out.print(" ");
}
}
}
System.out.println();
}
System.out.println("===========================");
}


public static void ten(String str) {
System.out.println(str);
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= i; j++) {
if (i < 3 || i == 10) {
System.out.print("#");
} else {
if (j == 1 || j == i) {
System.out.print("#");
} else {
System.out.print(" ");
}
}
}
System.out.println();
}
System.out.println("===========================");
}


public static void ele(String str) {
System.out.println(str);
int lay = 11;
for (int m = 1; m <= (lay + 1) / 2; m++)// 正序输出上半部分
{
for (int b = 1; b <= (lay + 1) / 2 - m; b++)// 输出空格
{
System.out.print(" ");
}
for (int c = 1; c <= m * 2 - 1; c++) {
if (c == 1 || c == (m * 2 - 1)) {
System.out.print("#");
} else {
System.out.print(" ");
}
}
System.out.println();// 换行
}
for (int d = (lay + 1) / 2 - 1; d >= 1; d--)// 倒序输出下半部分
{
for (int b = 1; b <= (lay + 1) / 2 - d; b++)// 输出空格
{
System.out.print(" ");
}
for (int c = (lay + 1) / 2 - d; c <= (lay + 1) / 2 - 2 + d; c++)// (lay+1)/2-1即为下半个三角形
{
if (c == (lay + 1) / 2 - d || c == (lay + 1) / 2 - 2 + d) {
System.out.print("#");
} else {
System.out.print(" ");
}
}
System.out.println();
}


}


public static void twe(String str) {
System.out.println(str);
int lay = 11;
for (int m = 1; m <= (lay + 1) / 2; m++)// 正序输出上半部分
{
for (int b = 1; b <= (lay + 1) / 2 - m; b++)// 输出空格
{
System.out.print(" ");
}
for (int c = 1; c <= m * 2 - 1; c++) {
System.out.print("#");
}
System.out.println();// 换行
}
for (int d = (lay + 1) / 2 - 1; d >= 1; d--)// 倒序输出下半部分
{
for (int b = 1; b <= (lay + 1) / 2 - d; b++)// 输出空格
{
System.out.print(" ");
}
for (int c = (lay + 1) / 2 - d; c <= (lay + 1) / 2 - 2 + d; c++)// (lay+1)/2-1即为下半个三角形
{
System.out.print("#");
}
System.out.println();
}
System.out.println("===========================");
}


}