Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JAVA for loop

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JAVA for loop

    hi to all..

    i would like to ask if this algorithm is accepted or is it a cheat? thanks

    here's my code
    import java.util.*;
    public class loop1
    {
    	public static void main (String[]args)
    	{
    		int x,y,limit;
     
    		Scanner input = new Scanner (System.in);
    		System.out.print("enter a limit: ");
    		limit = input.nextInt();
     
    		for(x=1;x<=limit;x++)
    		{
    			for(y=x;y<=limit;y++)
    			{
    				System.out.print(" "+y);
    			}	
     
    			System.out.println();
    		}	
     
    		for(x=limit-1;x>=1;x--)
    		{
    			for(y=x;y<=limit;y++)
    			{
    				System.out.print(" "+y);
    			}	
     
    			System.out.println();
    		}	
    	}
    }

    and the output should be like this

    *****
    ****
    ***
    **
    *
    **
    ***
    ****
    *****


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: JAVA for loop

    Hello, cheat?

    I'm not sure what you are on about, could you elaborate.

    // Json

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: JAVA for loop

    There are no cheats in programming... only odd hacks, strange (and possibly unstable) solutions, and anomalies.

    From reading your code, it looks like you want to print out a half hour-glass thing of numbers. That looks perfectly good to me.

Similar Threads

  1. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM
  2. [SOLVED] remembering the for loop initializer
    By lotus in forum Loops & Control Statements
    Replies: 13
    Last Post: July 17th, 2009, 11:29 PM
  3. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM
  4. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM
  5. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM