This is a very common starter question however I'm having trouble with the for loops themselves.
I am trying to complete the following question which asks to print the diamond shape you see below. The user needs to enter an odd number, and that is the number of asterisks that will be printed in the centre line.
A picture of the diamond shape that needs to be printed.
The code I originally wrote while thinking of how to do it is below. Now, I went through the code step-by-step and so I know the code is bad in relation to the question itself. What I'm struggling with is why the for loop isn't working.
package diamante;
import java.util.Scanner;
public class Diamente {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
try {
System.out.println("Enter an odd number for the size of the diamond");
n = sc.nextInt();
for (int i = 0; i >= n; i ) {
for (int j = (n / 2); j <= 0; j--) {
System.out.print(" ");
}
for (int k = 0; k >= i; k ) {
System.out.print("*");
}
n = n - 2;
i ;
System.out.println("test");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Nothing prints - not any spaces, nor an asterisk, nor the "Test". Likewise there are no error messages. Trying to find at what point it fails I took out all of the internal parts of the outer for loop, leaving just the following:
try {
System.out.println("Enter an odd number for the size of the diamond");
n = sc.nextInt();
for (int i = 0; i >= n; i ) {
System.out.println("test");
}
} catch (Exception e) {
e.printStackTrace();
}
But this still doesn't print anything. It seems like the most basic for loop you can write, so I don't understand why it's not working. I'm not getting any error messages from the catch part either, the program just stops with nothing happening at all.
Screenshot of the console after trying to run the code.
I knew you were able to set breakpoints and go through a program step by step, so I looked up how to do it quickly as I didn't know. I set the breakpoint at the for loop and ran the program as debug, and when I clicked to go into the loop, it jumps right over it.
Any clues as to what I'm missing would be greatly appreciated. It is baffling me.
CodePudding user response:
You had some mistakes in your for loops conditions.
You have to handle the "top" and "bottom of the diamond separately. There is a solution:
System.out.println("Enter an odd number for the size of the diamond");
n = sc.nextInt();
//Top part
for (int i = 1; i <= (n / 2) 1; i ) {
for (int j = 1; j <= n - i; j ) {
System.out.print(" ");
}
for (int j = 1; j <= (i * 2) - 1; j ) {
System.out.print('*');
}
System.out.println();
}
//Bottom part
for (int i = n / 2; i > 0; i--) {
for (int j = 1; j <= n - i; j ) {
System.out.print(" ");
}
for (int j = 1; j <= (i * 2) - 1; j ) {
System.out.print('*');
}
System.out.println();
}
CodePudding user response:
the written loop is wrong it should be less than or equal to <= not >= so it is not coming inside the first forloop itself,
you can refer the below code snippet:
public static void main(String[] args) { Scanner sc = new Scanner(System.in);
try {
System.out.println("Enter an odd number for the size of the diamond");
int m = sc.nextInt();
int n = m-1;
for (int i = 0; i <m; i ) {
for (int j = 0; j <n; j ) {
System.out.print(" ");
}
for (int k = 0; k <= i; k ) {
System.out.print("* ");
}
System.out.print("\n");
n--;
}
n = 0;
for (int i = m; i > 0; i--)
{
for (int j = 0; j < n; j )
System.out.print(" ");
// Print i stars
for (int j = 0; j < i; j )
System.out.print("* ");
System.out.print("\n");
n ;
}
} catch (Exception e) {
e.printStackTrace();
}`