user2725580
user2725580

Reputation: 1313

For loop output when scaling figure in java

I have been unable to get my for loop to run right number of times with my "figure" when scaling. The LINES constant here is the scaling "number".

The problem i am facing lies here i think:

for(int k = 0; k < LINES; k++){
    System.out.print("*******");
}

It is supposed to make a line of * at the bottom.

This is my whole code which produces a stairs figure of some kind

public class PP5 {
public static int j;
public static final int LINES = 5;
public static void main(String[] args) {
    for(j = 0 ; j < LINES; j++){

        fSpaces();
        System.out.print("  o   *******");
        bSpaces();
        System.out.println("*");
        fSpaces();
        System.out.print(" /|\\  *");
        bbSpaces();
        System.out.println("*");
        fSpaces();
        System.out.print(" / \\  *");
        bbSpaces();
        System.out.println("*");
    }
    for(int k = 0; k < LINES; k++){
        System.out.print("*******");
    }
}



public static void fSpaces(){
    for(int i = (LINES-1); i > j; i--){
        System.out.print("      ");
    }
}
public static void bSpaces(){
    for(int i = 0; i < j; i++){
        System.out.print("      ");
    }
}
public static void bbSpaces(){
    for(int i = 0; i < j+1; i++){
        System.out.print("      ");
    }
}
}

Any optimizations is highly appreciated. Thanks

Upvotes: 2

Views: 1277

Answers (3)

Tarsem Singh
Tarsem Singh

Reputation: 14199

you require 38 stars and you are printing 35

38=(6(Every increment) * 6 (No of times) )+2 (first increment is of 8[6+2])

No of times =6 Because indexing starts from 0 (0,1,2,3,4,5) so in actual counting is 6

so use

for(int k = 0; k <(LINES+1)*6; k++){
        System.out.print("*");
    }
    System.out.print("**");// last star

Output:

                          o   ********
                         /|\  *      *
                         / \  *      *
                    o   *******      *
                   /|\  *            *
                   / \  *            *
              o   *******            *
             /|\  *                  *
             / \  *                  *
        o   *******                  *
       /|\  *                        *
       / \  *                        *
  o   *******                        *
 /|\  *                              *
 / \  *                              *
**************************************

Upvotes: 3

blackbird014
blackbird014

Reputation: 2069

In your implementation just replace

for(int k = 0; k < LINES; k++){
    System.out.print("*******");
}

with

for(int k = 0; k < STEPS+1; k++){
    System.out.print("******");
}
System.out.print("**");

The motivation is that each step you add 7 * with one overlapping. This means that you need to add 6 * not 7. You add 1 time more to match the top part (but 2 * are missing: 1 because the top line is made of 7* and one for the column).

Upvotes: 1

Pshemo
Pshemo

Reputation: 124215

To get effect similar to this

                          o   ********
                         /|\  *     |*
                         / \  *     |*
                    o   *******     |*
                   /|\  *     |     |*
                   / \  *     |     |*
              o   *******     |     |*
             /|\  *     |     |     |*
             / \  *     |     |     |*
        o   *******     |     |     |*
       /|\  *     |     |     |     |*
       / \  *     |     |     |     |*
  o   *******     |     |     |     |*
 /|\  *     |     |     |     |     |*
 / \  *     |     |     |     |     |*
**************************************

|_____|_____|_____|_____|_____|_____|_

you need to notice that each of this part |_____

has six characters so you will need to use six * and print them LINES + 1 times since there are LINES + 1 |_____ parts.

This will generate

************************************|_

from

|_____|_____|_____|_____|_____|_____|_

so you will need to add last two * manually so change your last loop to

for (int k = 0; k <= LINES; k++) {
    System.out.print("******");//reduce star numbers by one
}
System.out.print("**");//and add this line

Upvotes: 2

Related Questions