user5346812
user5346812

Reputation:

How to print out 10 rows of 10 characters (or strings) in java non-tediously

I'm going through a beginners exercise for writing a small program in Java. My task is to print out (i.e. find an algorithm) the following output

XOOOOOOOOO
XXOOOOOOOO
XXXOOOOOOO
XXXXOOOOOO
XXXXXOOOOO
XXXXXXOOOO
XXXXXXXOOO
XXXXXXXXOO
XXXXXXXXXO
XXXXXXXXXX

I have figured out how to do it but the code I wrote seems repetitive and tedious. How can I shorten my code? There must be an easier way to achieve this?

Here is my code as of now;

package helloWorld;

public class HelloWorld {

public static void main(String[] args) 
{
    // Output


    // Algorithm 1

    boolean ft = true;

    String s = new String();
    for(int i = 0; i < 10; i++)
    {
        s  += "X";
        for(int j = 0; j < 9; j++)
        {
            if(i == 0)
            {

                s += "O";
            }

            if(i == 1)
            {

                if(ft == true)
                {
                    s+="X";
                    ft = false;
                }
                if(j == 8)
                {
                    continue;
                }
                s += "O";
            }

            if(i == 2)
            {

                if(ft == true)
                {
                    s+= "XX";
                    ft = false;

                }

                if(j == 7)
                {

                    break;
                }
                s += "O";

            }

            if(i == 3)
            {

                if(ft == true)
                {
                    s+= "XXX";
                    ft = false;

                }

                if(j == 6)
                {

                    break;
                }
                s += "O";
            }

            if( i == 4)
            {
                if(ft == true)
                {
                    s+= "XXXX";
                    ft = false;

                }

                if(j == 5)
                {

                    break;
                }
                s += "O";
            }

            if( i == 5)
            {
                if(ft == true)
                {
                    s+= "XXXXX";
                    ft = false;

                }

                if(j == 4)
                {

                    break;
                }
                s += "O";
            }

            if( i == 6)
            {
                if(ft == true)
                {
                    s+= "XXXXXX";
                    ft = false;

                }

                if(j == 3)
                {

                    break;
                }
                s += "O";
            }

            if( i == 7)
            {
                if(ft == true)
                {
                    s+= "XXXXXXX";
                    ft = false;

                }

                if(j == 2)
                {

                    break;
                }
                s += "O";
            }

            if( i == 8)
            {
                if(ft == true)
                {
                    s+= "XXXXXXXX";
                    ft = false;

                }

                if(j == 1)
                {

                    break;
                }
                s += "O";
            }

            if( i == 9)
            {
                if(ft == true)
                {
                    s+= "XXXXXXXXX";
                    ft = false;

                }

                if(j == 0)
                {

                    break;
                }
                s += "O";
            }


        }
        System.out.println(s);
        s = "";
        ft = true;

    }
}

}

Upvotes: 1

Views: 763

Answers (4)

Jens
Jens

Reputation: 69440

An other short Version using commons-lang3:

public static void main(String[] args) {
    // Algorithm 1
    String o = null;
    for (int i = 0; i < 10; i++) {
        String x = StringUtils.repeat("X", i+1);
        o =StringUtils.repeat("0", 9-i); 
        System.out.println(x+o);

    }

}

Upvotes: 0

Eugene
Eugene

Reputation: 11075

You can try the code below. In your code, I can't imagine how you will do that if the requirement is changed to a 100*100 square or larger.

public class HelloWorld {
    public static void main(String args[]){
        int size = 10;
        for(int i = 0; i < size; i++){
            int number_X = i;
            do{
                System.out.print("X");
                number_X--;
            }while(number_X >= 0);
            int number_O = size - i - 1;
            while(number_O > 0){
                System.out.print("O");
                number_O--;
            }
            System.out.println();
        }
    }
}

Upvotes: 0

SomeJavaGuy
SomeJavaGuy

Reputation: 7347

Just create a single loop, indicating how many rows you´d like to have.

In this loop you can nest two more loops, one looping to print all the X, one to print all the O.

public static void main(String[] args) {
    print(10);
}

private static void print(int length) {
    // Print until length 
    for (int i = 0;i<length;++i) {
        for (int j = 0;j<=i;++j) {
            // Write all the necessary X. Don´t print a carriage return
            System.out.print("X");
        }
        for (int j = i+1;j<length;++j) {
            // Write all the necessary O. Don´t print a carriage return
            System.out.print("O");
        }
        // Start a new line
        System.out.println();
    }
}

Upvotes: 0

uoyilmaz
uoyilmaz

Reputation: 3105

Just use 2 nested loops:

String s;
for( int i = 0; i < 10; i++)
{
    s = "";
    for( int j = 0; j <= i; j++)
        s+= "X";
    for( int j = i+1; j < 10; j++)
        s+= "O";
    System.out.println(s);
}

Upvotes: 5

Related Questions