1

I am working working on Chapter 2 Self-Check Problems from Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach. I am trying to get the output below vs. my code. I am trying to identify the line to ! to \ to / relationships and the math needed to compute the for loops. This is not homework and nor do I need an answer, direction or guidance is what I am seeking.

!!!!!!!!!!!!!!!!!!!!!!
\\!!!!!!!!!!!!!!!!!!//
\\\\!!!!!!!!!!!!!!////
\\\\\\!!!!!!!!!!//////
\\\\\\\\!!!!!!////////
\\\\\\\\\\!!//////////

My code as of now is:

/**
 * Created on 8/28/15.
 * Following Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach.
 * Chapter 2 Self-Check Problems
 */
public class Ch2_SelfCheckProblems {
    public static void main(String[] args) {
        question34();

    }

    public static void  question34(){
/**
* Table
* Line 1   ! = 22  \ = 0   / = 0
* Line 2   ! = 18  \ = 2   / = 2
* Line 3   ! = 14  \ = 4   / = 4
* Line 4   ! = 10  \ = 6   / = 6
* Line 5   ! = 6   \ = 8   / = 8
* Line 6   ! = 2   \ = 10  / = 10
*/

        for (int line = 1; line <= 6; line++){
            for (int i = 1; i <= 22; i++){
//                for (int j = 1; j <= (line - 1); j++){
//                    System.out.print("\");
//                }
                System.out.print("!");
            }
            System.out.println();
        }
    }



}
phillipsK
  • 1,466
  • 5
  • 29
  • 43
  • 2
    Answer: At line `line` what do you need to draw? how many `\`? How many `!` (22 - 2*(line -1)) etcetera. – Joop Eggen Aug 28 '15 at 12:18
  • My Tip : 3 inner loops. – zubergu Aug 28 '15 at 12:20
  • @JoopEggen Thanks that is the path I was after although `(22-2*(line-1))` only decrements `!` by a factor of two where I need to decrement by a factor of 4, resulting in a final line 6 containing only `!!` (two !) – phillipsK Aug 28 '15 at 12:44
  • @phillipsK please tell me if my answer is covering what you need – vefthym Aug 28 '15 at 13:48

4 Answers4

1

Try this: (I don't have a compiler at hand)

for (int line = 1; line <= 6; line++){
  for(int i = 1; i<=line-1; i++) {
    System.out.print("\\");
  }
  for (int i = 1; i <= 22 - 4*(line-1); i++){
    System.out.print("!");
  }
  for(int i = 1; i<=line-1; i++) {
    System.out.print("//");
  }
  System.out.println();
}

If you can't understand anything, leave a comment. em all ears.

Wololo
  • 841
  • 8
  • 20
  • Well your answer is definitely right, I am personally not looking to increment the variables outside of the for loops nor define variables outside the for loops as I have not yet progressively covered that in java yet, although I know your code logic works I just have not covered that yet thus trying to stay away from this approach, are you able to rewrite without doing this? – phillipsK Aug 28 '15 at 13:03
  • System.out.print("\\\\"); – phillipsK Aug 28 '15 at 18:46
1

It all depends on the number of lines that you wish to print. If you have x lines (here 6), then you can print what you wish, as follows:

int lines = 6;
for (int i = lines; i > 0; i--) { //start from the top line (6), finish at the lowest line (1)
    //print backslashes
    for (int back = 0; back < (lines-i)*2; back++) { //lines-i is the difference from the top line. add two extra slashes at each new line
        System.out.print("\\");
    }

    //print !s
    for (int up = 0; up < (i*4)-2; up++) {
        System.out.print("!");
    }

    //print slashes (as many as the backslashes)
    for (int forw = 0; forw < (lines-i)*2; forw++) {
        System.out.print("/");
    } 
    System.out.println();            
}

If you always want 6 lines, then just skip the int lines = 6; statement and replace lines with 6 everywhere.


So, at the first line, you print 4*x-2 '!'s and 0 '\'s and '/'s.
At the second line you print 4 less '!'s and 2 more '\'s and '/'s.
...
At the last line you print 2 '!'s and (x-1)*2 '\'s and '/'s.


In generall, the relationship that you are looking for, when you are given x lines, is the following:

At line i, counting from 1 (lowest) to x (top), print:
'\': (x-i)*2 times
'!': (i*4)-2 times
'/': (x-i)*2 times

vefthym
  • 7,422
  • 6
  • 32
  • 58
0

the answer is direct in this comment that says "table" in your code where your wrote the countings for the symbols. for loops are able to increase and decrease in any stepsize

for(int i = 22 ; i>2; i=i-4) for example

hope this was not to much.

hunta
  • 1
0
/**
 * Created on 8/28/15.
 * Following Reges, Stuart, and Martin Stepp. Building Java Programs: A Back to Basics Approach. 3rd Edition.
 * Chapter 2 Self-Check Problems Question 34 & 35
 */
public class Ch2_SelfCheckProblems {

    public static final int SIZE = 4;

    public static void main(String[] args) {
        System.out.println("Practice:");
        question34();
        System.out.println("Partially correct:");
        q34();
        System.out.println("Solution, scalable with constant:");
        solution34();

    }

    /**
     * Table 6 lines; CONSTANT = 6
     * Line 1   ! = 22  \ = 0   / = 0
     * Line 2   ! = 18  \ = 2   / = 2
     * Line 3   ! = 14  \ = 4   / = 4
     * Line 4   ! = 10  \ = 6   / = 6
     * Line 5   ! = 6   \ = 8   / = 8
     * Line 6   ! = 2   \ = 10  / = 10
     *
     * Table 4 lines; CONSTANT = 4
     * Line 1   ! = 14  \ = 0   / = 0
     * Line 2   ! = 10  \ = 2   / = 2
     * Line 3   ! = 6   \ = 4   / = 4
     * Line 4   ! = 2   \ = 6   / = 6
     */

    public static void  question34(){

        for (int line = 1; line <= 6; line++){
            for (int i = 1; i <= (22-2*(line-1)); i++){
//                for (int j = 1; j <= (line - 1); j++){
//                    System.out.print("\");
//                }
                System.out.print("!");
            }
            System.out.println();
        }
    }

    public static void q34(){
        for (int line = 1; line <= 6; line++){
            for(int i = 1; i<=line-1; i++) {
                System.out.print("\\\\");
            }
            for (int i = 1; i <= 22 -(4*(line-1)); i++){
                System.out.print("!");
            }
            for(int i = 1; i<=line-1; i++) {
                System.out.print("//");
            }
            System.out.println();
        }
    }

    public static void solution34(){
        for (int line = 1; line <= SIZE; line++){
            for(int i = 1; i<=((2 * line) - 2); i++){
                System.out.print("\\");
            }
            for (int i = 1; i <= ( -4 * line + ( 4 * SIZE + 2 ) ); i++){
                System.out.print("!");
            }
            for(int i = 1; i<= ((2 * line) - 2); i++){
                System.out.print("/");
            }
            System.out.println();
        }
    }
}
phillipsK
  • 1,466
  • 5
  • 29
  • 43