0

Yes, this is homework, but I cant figure out how to do it using only for loops.

enter image description here

I was able to reproduce the structure with a for loop and if else statements but my instructor told me that won't fly.

public class Problem3 {
public static void main(String[] args) {
    /** Code under here */
        int box = 22;
        for(int i=22; i>0; i--)
        {
         if(i==18)
             System.out.println("\\\\"+"!!!!!!!!!!!!!!!!!!"+"//");
         else if(i == 14) 
             System.out.println("\\\\\\\\"+"!!!!!!!!!!!!!!"+"////");
         else if (i== 10)
             System.out.println("\\\\\\\\\\\\"+"!!!!!!!!!!"+"//////");
         else if (i == 6)
             System.out.println("\\\\\\\\\\\\\\\\"+"!!!!!!"+"////////");
         else if (i == 2)
             System.out.println("\\\\\\\\\\\\\\\\\\\\"+"!!"+"//////////");
         else if(i == 22)
             System.out.println("!!!!!!!!!!!!!!!!!!!!!!");
        }


   System.out.println("");
}    
}
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Christopher Baldwin
  • 347
  • 2
  • 4
  • 15

6 Answers6

4

Some hints:

  • The lines are always symetric
  • The number of \ at the beginning of a line can be calculated from the line number (starting at 0)
  • The number of characters on a line is constant, so the number of ! can also be calculated if you know how many \ there are.

Based on these observations, you should be able to produce a code with only 2 levels of nested loops (I didn't say 2 loops, just 2 levels).

Kaidjin
  • 1,433
  • 1
  • 12
  • 18
  • I just wanted to say thanks. I can't upvote your post because im too new. – Christopher Baldwin Oct 21 '13 at 15:35
  • You're welcome. You can probably accept the answer though, to let everyone know that you found a solution to your problem. – Kaidjin Oct 21 '13 at 15:37
  • I'm still trying to figure it out though :P Don't worry, I'll give you your just rewards. From the code 'given' below by Muhammad, I got the idea to just build each part separately. Would that work? Or will the box not form correctly. – Christopher Baldwin Oct 21 '13 at 15:41
  • Yes, you have to build each part of the line separately. The trick is to figure out the rules which defines the lines in function of the number of the line. – Kaidjin Oct 21 '13 at 15:57
1

There are 22 characters in a row, and row n has 2*(n-1) \ characters, 2*(n-1) / characters and 22 - 4*(n-1) ! characters.

You should first build the string you want to print out (for each line). That will involve repeating the characters (for which this question provides a few ideas)

Community
  • 1
  • 1
SheetJS
  • 22,470
  • 12
  • 65
  • 75
0

Notice that in each row i (if you number them from 0) there are 2 * i \ characters at the start, 2 * i '/' characters at the end and the rest of the row is filled with '!' characters.

So your code will look something like:

for ( int row = 0; row < 6; row++ ) {
  int slashes = 2 * row;
  for ( int slash = 0; slash < slashes; slash++ ) {
    // Print a `\`
  }
  // Work out how many '!'s you need and print them.
  // ... you try this bit
  for ( int slash = 0; slash < slashes; slash++ ) {
    // Print a `/`
  }
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • I see that, but how do I get nested for loops to print the different parts of the triangle? Actually @Muhammad-S 's answer that's being mercilessly downvoted is starting to make sense when I combine it with your explanation. Should I build each part separately – Christopher Baldwin Oct 21 '13 at 15:39
0

Notice that each time, there are two more slashes/backslashes on each side (and thus four less !s in the middle). You can make a for loop that increases the number of slashes, and calculates the number of !s to print to fill out the length to 22, and then print out all necessary characters for the current line.

Tim S.
  • 55,448
  • 7
  • 96
  • 122
0

Is this good enough? I'm learning java too. Feedback appreciated.

    public class Problem3 {
public static void main(String[] args) {
    /** Code under here */
    int box = 22;
    int line = 0;
    String char_1 = "w";
    String char_2 = "q";
    String char_3 = "e";
    String string = "";
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < line * 2; j++) {
            System.out.print(char_1);
        }
        for (int k = box; k > 0; k --) {
            System.out.print(char_2);
        }
        for (int l = 0; l < line * 2; l++){
            System.out.print(char_3);
        }
        System.out.println(string);
        line++;
        box = box - 4;
    }

}

}

Pedro Gonzalez
  • 1,429
  • 2
  • 19
  • 30
-1

The number_of_lines is the number of lines inside the expected output (which is 6 in this case). The number_of_characters is the number of characters each line should have (which is 22 in this case).

We know that there is a pattern. The left side has \, the middle has !, and the right side has /. This means we need one outer loop to loop through the lines and three inner loops for each of these sections.

How do we get know how many of each characters we need?

The first line has 0 \, 22 !, and 0 /

The second line has 2 \, 18 !, and 2 /

The third line has 4 \, 14 !, and 4 /

The fourth line has 6 \, 10 !, and 6 /

The fifth line has 6 \, 10 !, and 6 /

The sixth line has 8 \, 6 !, and 8 /

The seventh line has 10 \, 2 !, and 10 /

There is a pattern. Each line has the \ and / count increment by 2 and the ! count decrement by 4.

Number of \ equals two times the current line number (assuming the first line number is 0). Same goes with /. The number of ! is the total number of characters expected per line minus four times the current line number (assuming the first line number is 0).

public void printCoolStuff(int number_of_lines, int number_of_characters){

    for(int i = 0; i < number_of_lines; i ++){

        String left= "";
        for(int j = 0; j < (2*i); j++){
            left= left+ "\"
        }

        String middle = "";
        for(int j = 0; j < (number_of_characters - 4*i); j++){
            middle = middle + "!"
        }

        String right= "";
        for(int j = 0; j < (2*i); j++){
            right = right + "/"
        }

        System.out.println(left+middle+right);
    }
}