Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
Nested For-Loop Explanation

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();
  }
}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda