mercredi 31 janvier 2018

Square pattern java

Create void method that will put on screen square with patern like that :

  xoxo  
  xoxo   
  xoxo   
  xoxo

First argument of the method will define amount of characters used to create a square side, second which character is first.

This is my solution but im wondering if i can do it with less code.

static void square(char a, int b) {
 if (a == 'x') {
        for (int i = 0; i < b; i++) {

            int sum = 0;
            do {


                System.out.print("x");
                sum++;
                if (sum == b)
                    break;
                System.out.print("o");
                sum++;

            }
            while (sum != b);

            System.out.println();
        }

    } else {
        for (int i = 0; i < b; i++) {

            int sum = 0;
            do {


                System.out.print("o");
                sum++;
                if (sum == b)
                    break;
                System.out.print("x");
                sum++;

            }
            while (sum != b);

            System.out.println();
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire