Ascii Art with Java-创建具有不同符号边缘的正方形/矩形

问题描述 投票:1回答:1

我正在打印输出从控制台输入的两个整数,然后从这两个整数(然后是尺寸)显示矩形或正方形的ascii艺术。但是拐角必须是与主要符号不同的符号...但是诀窍是,短边上必须仅具有1或2个原始符号(由于奇数或偶数)。

这里有两个例子:

6x9:

001111100
011111110
111111111
111111111
011111110
001111100

9x6:

001100
011110
111111
111111
111111
111111
111111
011110
001100

我已经走了这么远(因为控制台仅从0变到9,对吧?)考虑到角落,需要添加什么?If语句会起作用还是其他?是的,我知道这仅适用于“正方形”。如何添加第二个维度?我可以帮忙吗?

class Main {
public static void printSquare(int size) {
    if(size > 9) {
       size = 9;
    }
    int line = 1;

        while (line <= size) { 
            int width = size; 
            int i = 1; 

            while (i <= width) {
                System.out.print("*");
                i = i + 1;
            }

            System.out.println(); // Newline
            line = line + 1;
        }
    }
}
java ascii ascii-art
1个回答
0
投票

您只需简单地告诉我们三个角符号是不同的。

 Scanner keys = new Scanner(System.in);

 int x = 0;
 int y = 0;

 public void getInput() {

 x = keys.nextInt();
 y = keys.nextInt();
 createart();

 }

 public void createart() {

 System.out.print("00");

 int counter = 0;

 while (counter < x - 4) {

 System.out.print(1);

 counter++;

 }
 System.out.println("00");

 counter = 0;
 System.out.print("0");
 while (counter < x - 2) {

 System.out.print(1);
 counter++;

 }
 System.out.print("0");
 counter = 0;
 int counter2 = 0;
 while (counter < y - 4) {
 System.out.println("");

 while  (counter2 < x) {

 System.out.print(1);

 counter2++;
 }
 counter++;
 } 
 System.out.println("");
 counter = 0;
 while (counter < x - 2) {

 System.out.print(1);

 counter++;

 }

 counter = 0;
 System.out.println("0");
 System.out.print("00");
 while (counter < x - 4) {

 System.out.print(1);

 counter++;

 }

 System.out.print("00"); 

 }   

简单的逻辑。

© www.soinside.com 2019 - 2024. All rights reserved.