Marie Mirbekian Marie Mirbekian - 4 months ago 11
Java Question

My code works for printing "*"s but not my desired pattern

my code works for printing 3 by 3 star pattern. But when I want to replace it with my desired one, it doesn't work. And I included the output that I get as well as the output I want to get

/*
* * *
* * *
* * * /*

/* The pattern I want to make is:
+--+--+--+
| | | |
+--+--+--+
| | | |
+--+--+--+
| | | |
+--+--+--+ */

The output that I get:
*--*
| |*--*
| |*--*
| |
*--*
| |*--*
| |*--*
| |
*--*
| |*--*
| |*--*
| |

public class HelloWorld
{
public static void main(String[] args)
{

for( int i = 0; i<=2; i++){
for(int j =0; j<=2; j++){
System.out.print("*--*\n| |");
}
System.out.println();

}
}
}

Answer

try this

public static void main(String[] args) {

        for (int i = 0; i <= 2; i++) {
            for (int j = 0; j <= 2; j++) {
                System.out.print("*--");
            }
            System.out.println("*");
            for (int j = 0; j <= 2; j++) {
                System.out.print("|  ");
            }
            System.out.println("|");
        }

        for (int j = 0; j <= 2; j++) {
            System.out.print("*--");
        }
        System.out.println("*");
    }

Your code doesn't work, because you took several characters (which are not even positioned next to each other in line) as one.

Comments