can someone please tell me what im doing wrong

this code works:

String r  = "";
         for(int i =1; i <= rows; i++)
                {
                        for(int j = 1; j <=columns; j++)
                            {
                             r = r + " _";
                             r = r + "\n";
                            }
                         return r;
                 }

This code works and i get a square grid.

String r  = "";
         for(int i =1; i <= rows; i++)
                {
                        for(int j = 1; j <=columns; j++)
                            {
                            if (( x1 == j) && (y1 == i))
                            {
                            r = r + " X";                    
                            }
                            else
                        {
                        r = r + " _";  
                        r  = r + "\n";
                    }
                }
            }
            
            return r ;

The above code does not work and i don't know why, i want to make the same square grid but when the If condition is true i want to replace _ with X. Thx in advance for the help

Recommended Answers

All 3 Replies

Both pieces of code say they work above the code. Which one doesn't work? The second one? And can you please fix your bracketing, I can't tell which brackets go with what. The brackets should be aligned so that the closing bracket is in the same column as the opening bracket.

Both pieces of code say they work above the code. Which one doesn't work? The second one? And can you please fix your bracketing, I can't tell which brackets go with what. The brackets should be aligned so that the closing bracket is in the same column as the opening bracket.

bottom one doesnt work

String r  = "";
         for(int i =1; i <= rows; i++)
                {
                        for(int j = 1; j <=columns; j++)
                            {
                             if (( x1 == j) && (y1 == i))
                                {
                                 r = r + " X";                    
                            }
                                 else
                                 {
                                      r = r + " _";  
                                      r  = r + "\n";
                                  }
                                  }
                }
            
            return r ;

None of your codes are correct. As stated in your previous thread by me, your initial code was correct. But it seems that you followed an incorrect suggestion by another member. The new line character should be placed outside the inner loop:

String r  = "";
         for(int i =1; i <= rows; i++)
                {
                        for(int j = 1; j <=columns; j++)
                            {
                             r = r + " _";
                             //[B]r = r + "\n";[/B]
                            }
                            [B]r = r + "\n";[/B]
                         // [B]return r;[/B]
                 }
[B]return r;[/B]

And of course the return must be at the end of your program, outside the outer loop. With your code, you return the moment the inner loop finishes the first time and the outer loop doesn't get a chance to finish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.