Results 1 to 8 of 8
  1. #1
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Need some help with number diamond and explanation of code

    Hey all here's my code:
    Java 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:


    Java 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

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Need some help with number diamond and explanation of code

    Quote Originally Posted by codyjava View Post
    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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Need some help with number diamond and explanation of code

    Quote Originally Posted by jim829 View Post
    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

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Need some help with number diamond and explanation of code

    Hey Jim so this is how you would count down:
    Java 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:
    Java 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

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Need some help with number diamond and explanation of code

    Alright this is what I ended up with:
    Java 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:
    Java Code:
       1
      121
     12321
    1234321
     12321
      121
       1
    Thanks for all the help

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Need some help with number diamond and explanation of code

    Great!

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Explanation of Java code
    By ubuntu_jdk in forum AWT / Swing
    Replies: 3
    Last Post: 05-29-2013, 10:59 PM
  2. Explanation of Java Zip code
    By joshhazel in forum New To Java
    Replies: 0
    Last Post: 01-25-2012, 03:59 AM
  3. some code explanation please?
    By javawreker in forum New To Java
    Replies: 14
    Last Post: 12-31-2011, 07:05 PM
  4. Need a java code and explanation for this
    By Crossover in forum New To Java
    Replies: 4
    Last Post: 09-21-2010, 02:23 AM
  5. need code explanation
    By marie in forum New To Java
    Replies: 3
    Last Post: 03-05-2010, 01:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •