javadrawfigures

Draw figure from java textbook


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();
        }
    }



}

Solution

  • 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.