# Need some help with number diamond and explanation of code

• 09-15-2013, 01:11 AM
codyjava
Need some help with number diamond and explanation of code
Hey all here's my code:
Code:

```public static void main(String [] args){                                 Scanner scan = new Scanner(System.in);                                 int n;                                 System.out.print("Enter an interger:");                 n = scan.nextInt();                                                                 for (int i = 1; i <= n; i++)         {               for (int j = 0; j < (n - i); j++)                     System.out.print(" ");               for (int x = 1; x <= i; x++)                     System.out.print((x >=n) ?+ x : + x);               for (int k = 1; k < i; k++)                     System.out.print((k >= n) ?+ k : + k);               System.out.println();         }         for (int i = n - 1; i >= 1; i--)         {               for (int j = 0; j < (n - i); j++)                     System.out.print(" ");               for (int x = 1; x <= i; x++)                     System.out.print((x >=n) ?+ x : + x);               for (int k = 1; k < i; k++)                     System.out.print((k >= n) ?+ k : + k);               System.out.println();         }         System.out.println();   } }```
So it works but it outputs something like this if user enters 3 for example:

Code:

```   1  121 12312  121   1```

Well I want it to go 12321 on the third line so basically counts down. I was wondering whats the best what to do this?

Also could some help me understand what System.out.print((x >=n) ?+ x : + x); is actually doing.

Thanks for anyhelp
• 09-15-2013, 02:28 AM
jim829
Re: Need some help with number diamond and explanation of code
Quote:

Originally Posted by codyjava
Also could some help me understand what System.out.print((x >=n) ?+ x : + x); is actually doing.

You did write this code yourself, correct? I ask this because it is strange for someone to put something in their code and not know why. In any case, your construct above just prints out x. Same for the other uses of that statement.

What it means is:

(if conditional is true) ? do this : otherwise, do this.

Regards,
Jim
• 09-15-2013, 02:32 AM
codyjava
Re: Need some help with number diamond and explanation of code
Quote:

Originally Posted by jim829
You did write this code yourself, correct? I ask this because it is strange for someone to put something in their code and not know why. In any case, your construct above just prints out x. Same for the other uses of that statement.

What it means is:

(if conditional is true) ? do this : otherwise, do this.

Regards,
Jim

Hey Jim thanks for that answer and to answer your question I wrote all of it but the part I asked about. I found that from another example. Also do you have any input on the counting backwards part?

Thanks
• 09-15-2013, 02:47 AM
jim829
Re: Need some help with number diamond and explanation of code
You know how to count forward from 1 to 5 in a for loop. That would give you 12345. So on the same line, how would
you count backward to make the line symmetric to give you 123454321?

Regards,
Jim
• 09-15-2013, 03:27 AM
codyjava
Re: Need some help with number diamond and explanation of code
Hey Jim so this is how you would count down:
Code:

```        public static void main(String [] args){                 for(int i = 1; i<7;i++){                                 for (int l = 7; l >= i; l--) {                         System.out.print(l);                 }                 System.out.println();                 }         } }```
Output:
Code:

```7654321 765432 76543 7654 765 76```
The problem is though I need it to display that backwards, so the numbers start shorter at the top and longer at the bottom. Also I'm just using the number 7 (int = 7), so this would only work if the user entered in the number 8. How could I get a number that changed fro the user input.

Sorry if this is confusing
Thanks
• 09-15-2013, 03:55 AM
jim829
Re: Need some help with number diamond and explanation of code
Break this task up into smaller tasks. First, try and just get numbers to print bottom to top to bottom in a single row. Like this:

123454321

Hint. Use two loops. First goes up, second goes down.

Then put both of those inside a main loop to get the following:

1
121
12321
1234321
123454321

Worry about spaces later. It also helps to work this out on paper to see how the variables might be set.

Regards,
Jim
• 09-15-2013, 04:43 AM
codyjava
Re: Need some help with number diamond and explanation of code
Alright this is what I ended up with:
Code:

```        public static void main(String [] args){                                 Scanner scan = new Scanner(System.in);                                 int n;                                 System.out.print("Enter an interger:");                 n = scan.nextInt();                                                 //top                 for (int i = 1; i <= n; i++)         {               for (int j=0; j<(n-i) ; j++)                     System.out.print(" ");               for (int x=1; x<=i; x++)                     System.out.print(x);               for (int k=i-1; k>0; k--)                     System.out.print(k);               System.out.println();         }                 //bottom         for (int i=n-1; i>=1; i--)         {               for (int j = 0; j < (n - i); j++)                     System.out.print(" ");               for (int x = 1; x <= i; x++)                     System.out.print(x);               for (int k=i-1; k>0; k--)                     System.out.print(k);               System.out.println();         }         System.out.println();   } }```
Here's the output:
Code:

```  1   121  12321 1234321  12321   121   1```
Thanks for all the help
• 09-15-2013, 04:56 AM
jim829
Re: Need some help with number diamond and explanation of code
Great!

Regards,
Jim