Home > Back-end >  Nested For-Loop Explanation
Nested For-Loop Explanation

Time:10-05

Code written in java

I'm trying to print the user's input amount of "*" on the same line.

Here is what I have:

if (((input / 2)   1) == ir) {
    for (int ij = 1 ; ij <= input; ij  ) {
        System.out.print("*");
    }
}

the if statement is testing to see if we are at the halfway point of the shape I'm trying to make (a bowtie).

I feel like my logic and code is correct but for an input of 5,

this particular line looks like this: ***

instead of: *****

Can someone explain to me why this is happening?

Here is the full code:

import java.util.Scanner;

public class BowTie {
  public static void main(String [] args) {
    Scanner scnr = new Scanner(System.in);
    int input = scnr.nextInt();

    int stars = 1;
    int spaces = input - 2;
    if ((input % 2 == 1) && (input >= 1)) {
        for (int ir = 1; ir <= input; ir  ) {
            for (int ic = 1; ic <= stars; ic  ) {
                System.out.print("*");
            }
            for (int ic = 1; ic <= spaces; ic  ) {
                if (((input / 2)   1) == ir) {
                    for (int ij = 1; ij <= input; ij  ) {
                        System.out.print("*");
                    }
                } else {
                    System.out.print(" ");
                }
            }
            if (((input   1) / 2) != ir) {
                for (int ic = 1; ic <= stars; ic  ) {
                    System.out.print("*");
                }
            }
            if ((input / 2) < ir) {
                stars--;
                spaces  = 2;
            } else {
                stars  ;
                spaces -= 2;
            }
            System.out.println();
        }
    } else {
        return;
    }
    scnr.close();
  }
}

CodePudding user response:

This is because the checks you used. To be short, check this modified script:

import java.util.Scanner;

public class BowTie {
  public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    int input = scnr.nextInt();

    int stars = 1;
    int spaces = input - 2;
    if ((input % 2 == 1) && (input >= 1)) {
        for (int ir = 1; ir <= input; ir  ) {
          
            if (((input   1) / 2) != ir) { // add this check
              for (int ic = 1; ic <= stars; ic  ) {
                  System.out.print("*");
              }
            }
            // change this logic: ---
            if (((input   1) / 2) == ir) {
              for(int ic = 1; ic <= input; ic  ) {
                System.out.print("*");
              }
            } else {
              for (int ic = 1; ic <= spaces; ic  ) {
                System.out.print(" ");
              }
            }
            // ---
            if (((input   1) / 2) != ir) {
                for (int ic = 1; ic <= stars; ic  ) {
                    System.out.print("*");
                }
            }
            if ((input / 2) < ir) {
                stars--;
                spaces  = 2;
            } else {
                stars  ;
                spaces -= 2;
            }
            System.out.println();
        }
    }
    scnr.close();
  }
}

(lines with comments was the changes)

The first for inside for (int ir = 1; ir <= input; ir ) was printing the stars you see.
Also, the middle part was changed to print the entire line, or the spaces (like you did).

CodePudding user response:

You are missing the else condition on if (((input 1) / 2) != ir) as follows:

import java.util.Scanner;

public class BowTie {
  public static void main(String [] args) {
    Scanner scnr = new Scanner(System.in);
    int input = scnr.nextInt();

    int stars = 1;
    int spaces = input - 2;
    if ((input % 2 == 1) && (input >= 1)) {
        for (int ir = 1; ir <= input; ir  ) {
            for (int ic = 1; ic <= stars; ic  ) {
                System.out.print("*");
            }
            for (int ic = 1; ic <= spaces; ic  ) {
                if (((input / 2)   1) == ir) {
                    for (int ij = 1; ij <= input; ij  ) {
                        System.out.print("*");
                    }
                } else {
                    System.out.print(" ");
                }
            }
            if (((input   1) / 2) != ir) {
                for (int ic = 1; ic <= stars; ic  ) {
                    System.out.print("*");
                }
            // Added else
            } else {
                for (int ic = 1; ic <= (input - ir); ic  ) {
                    System.out.print("*");
                }
            }
            if ((input / 2) < ir) {
                stars--;
                spaces  = 2;
            } else {
                stars  ;
                spaces -= 2;
            }
            System.out.println();
        }
    } else {
        return;
    }
    scnr.close();
  }
}

Nevertheless, your code is a bit hard to read and understand. You might need to refactor it a bit.

  • Related