Question

I'm working to printout ascii art which takes two integers entered from the console then displays a rectangle or square from those two integers (the then dimensions). But the corners need to be a different symbol then the main symbol... yet the trick is that the short side has to have only 1 or 2 of the original symbols on it (due to odd or even.)

Here are two examples:

6x9:

001111100
011111110
111111111
111111111
011111110
001111100

9x6:

001100
011110
111111
111111
111111
111111
111111
011110
001100

I've gotten this far (since the console only goes from 0 to 9 right?) What would need to be added to take in account the corners? Would an If statement work or something else? And yes, I know this is only for the "square". How would I add a second dimension? Can I get some help?

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;
        }
    }
}
Was it helpful?

Solution

You need to simply tell it that the three corner symbols are different.

 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"); 

 }   

Simple logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top