user11301210
user11301210

Reputation:

When developing a nested for loop, how do i decide the conditions to be used?

My output is not lining up correctly. The top is my target, while my output is the bottom. For this specific example the width equals 8. How should I create the conditions for my for loop?

I am a beginner developer and I have not learned discrete math yet. I believe my problem occurs in the third for loop. I want my spacing to be 0, 2, 4, etc.. How should I approach these types of problems?

The expected output:

        /\   
       /  \  
      /    \ 
     /      \
     ________

The code:

public static void methodName(int width) {
  for(int a = 0; a < width / 2 + 1; a++ ) {
    for(int b = width - 2; b > a; a--) {
      System.out.print(" ");
    }

    System.out.print("/"); 

    for(int c = 0; c < a; c++) {
      System.out.print(" ");
    }

    System.out.println("\\");
  }

  for(int d = 0; d < width; d++) {
    System.out.print("_");
  }

  System.out.println();
}

Actual output:

       /\
      / \
     /  \
    /   \
   /    \
 ________

Upvotes: 0

Views: 91

Answers (4)

Sarel Foyerlicht
Sarel Foyerlicht

Reputation: 927

working code:

public static void methodName(int width){

        for(int a = 0; a < width / 2 + 1; a++ ) {
            for(int b =0; b < width/2-a;b++) {//get the middle - our postion
                System.out.print(" ");
            }

            System.out.print("/");

            for(int c = 0; c < (width-(width-a))*2; c++) {//Just the opposite
                System.out.print(" ");
            }

            System.out.println("\\");
        }

        for(int d = 0; d < width+2-width%2; d++) {//odd or even numbers has /2 different
            System.out.print("_");
        }

        System.out.println();
    }

(Another suggestion while debugging use "|" instead of " ", its much more easier to see and understand what is going on)

Upvotes: 1

J Mers
J Mers

Reputation: 67

Try to devide the problem into small chunks and analyse them each one by one

  1. Look at the result and try to find the difference to what you expected it to be. Is there an error-pattern? Which part fails, which part works fine?

  2. Try to connect from the parts(3 lines and 2 blank spaces) of the result to the code which produces them.

  3. Fix the frist part you find an error in, then go to the next one

In your case:

  1. left blank space looks good

  2. left line looks good

  3. middle blank space seems to be wrong.

  4. right line goes down straight instead of going to the right(ignore for now, first fix 3. then look back again)

...

(Won't provide complete solution due to it propably beeing "homework"

Upvotes: 0

mcacorner
mcacorner

Reputation: 1274

Have a look to generate two side of triangle help your self for remaining side and debug program to solve it.

for(int i=WIDTH; i>=0;i--) {            
    for(int j=0;j<=i;j++) {
        System.out.print(" ");
    }
    System.out.print("/");
    for(int k=i;k<WIDTH;k++) {
        System.out.print("  ");
    }
    System.out.println("\\");
}

Upvotes: 0

bsheps
bsheps

Reputation: 1544

How should I approach these types of problems?

I would suggest approaching the problem by stepping through the code either on a piece of paper, a white board or using a debugger. See how your actual output differs from your expected output. It never hurts to draw the output or write out the code.

How?

Take a value for width and plug it in. Step through the code, one line at a time, and keep track of all your local variables like a, b, etc. Eventually you will come across the code where your expected output is different from your actual output. It should be pretty self explanatory from here.

Note: I think this is homework so I don't want to do the problem for you.

Upvotes: 1

Related Questions