有大佬知道怎么
根据现有代码 创建一个封装 打印五个这样的小长城连接在一块 就类似于长城那样该怎么弄吗 在线求源码
package com.Lxsibianxing;
public class shiChangCheng {
public static void main(String[] args)
{
for (int i=1;i<=1;i++) {
new shiChangCheng().myPrint(i);
}
}
public void myPrint(int count)
{
for (int row = 1; row <= 10; row++) {
if (row >= 1 && row <= 4) {
for (int col = 1; col <= 21; col++) {
if (col >= 6 && col <= 10) {
if (row == 3 && col == 8) {
System.out.print(" " + " ");
} else {
System.out.print("*" + " ");
}
} else {
System.out.print(" " + " ");
}
}
System.out.println();
}
if (row == 5) {
for (int col2 = 1; col2 <= 15; col2++)
if (col2 >= 7 && col2 <= 9) {
System.out.print(" " + " ");
} else {
System.out.print("*" + " ");
}
System.out.println();
}
if (row >= 8) {
for (int col3 = 1; col3 <= 15; col3++)
if (col3 >= 2 && col3 <= 14) {
System.out.print(" " + " ");
} else {
System.out.print("*" + " ");
}
System.out.println();
}
if(row == 10) {
for (int col4 =1; col4 <=20;col4++)
if (col4 >= 2 && col4 <=14) {
System.out.print(" "+" ");
} else {
System.out.print("*"+" ");
}
System.out.println();
}
}
}
}
连一起平移就可以了,定义循环次数,例如:
for(int i = 0;i<5;i++){
for(int col = i * 20 +0; col < i * 20 +21; col ++){
System.out.print("*");
}
}
System.out.println();
public class shiChangCheng {
public static void main(String[] args) {
new shiChangCheng().myPrint(5);
}
public void myPrint(int count) {
for (int row = 0; row < 9; row++) {
if (row < 4) {
for (int i = 0; i < count; i++) {
for (int col = 0; col < 20; col++) {
if (col > 4 && col < 10) {
if (row == 2 && col == 7) {
System.out.print(" " + " ");
} else {
System.out.print("*" + " ");
}
} else {
System.out.print(" " + " ");
}
}
}
System.out.println();
}
if (row == 4) {
for (int i = 0; i < count; i++) {
for (int col2 = 0; col2 < 20; col2++) {
if (col2 > 5 && col2 < 9) {
System.out.print(" " + " ");
} else if (col2 > 14) {
System.out.print(" " + " ");
} else {
System.out.print("*" + " ");
}
}
}
System.out.println();
}
if (row > 4 && row < 8) {
for (int i = 0; i < count; i++) {
for (int col3 = 0; col3 < 20; col3++) {
if (col3 > 0 && col3 < 14) {
System.out.print(" " + " ");
} else if (col3 > 14) {
System.out.print(" " + " ");
} else {
System.out.print("*" + " ");
}
}
}
System.out.println();
}
if (row == 8) {
for (int i = 0; i < count; i++) {
for (int col4 = 0; col4 < 20; col4++) {
if (col4 > 0 && col4 < 14) {
System.out.print(" " + " ");
} else {
System.out.print("*" + " ");
}
}
}
System.out.println();
}
}
}
}