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 9 of 9

Thread: I need to create a certain SHAPE with FOR LOOP.

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need to create a certain SHAPE with FOR LOOP.

    This was the proposed output:
    ************
     ************
    ************
     ************
    ************
     ************
    ************
     ************

    i cant do it purely with for loop i dont know if im missing something
    public class Loop {
     
    	public static void main(String[] args) {
     
    		for (int i = 1; i <= 8; i++) {
    			switch(i){
    			case 2:
    				System.out.print(" ");
    				break;
    			case 4:
    				System.out.print(" ");
    				break;
    			case 6:
    				System.out.print(" ");
    				break;
    			case 8:
    				System.out.print(" ");
    				break;
    			}
     
     
    			for (int j = 0; j < 12; j++) {
    				System.out.print("*");				
    			}
    			System.out.println();
    		}
     
    	}
     
    }


    Anyone else have other methods just only using forloop? please be humble with me its my first time asking a community im nervous.


  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need to create a certain SHAPE with FOR LOOP.

    Quote Originally Posted by Rage1337 View Post
    This was the proposed output:
    ************
     ************
    ************
     ************
    ************
     ************
    ************
     ************

    i cant do it purely with for loop i dont know if im missing something
    public class Loop {
     
    	public static void main(String[] args) {
     
    		for (int i = 1; i <= 8; i++) {
    			switch(i){
    			case 2:
    				System.out.print(" ");
    				break;
    			case 4:
    				System.out.print(" ");
    				break;
    			case 6:
    				System.out.print(" ");
    				break;
    			case 8:
    				System.out.print(" ");
    				break;
    			}
     
     
    			for (int j = 0; j < 12; j++) {
    				System.out.print("*");				
    			}
    			System.out.println();
    		}
     
    	}
     
    }


    Anyone else have other methods just only using forloop? please be humble with me its my first time asking a community im nervous.
    Hiya,

    is this answer too obvious?
    public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		for(int i=0; i<5; i++){
    			if (i % 2 == 0)
    				System.out.println(" ************");
    			else
    				System.out.println("************");
    		}
     
    	}
    regards,

    Kevin.
    Last edited by wrightkevin; August 5th, 2012 at 01:03 PM.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need to create a certain SHAPE with FOR LOOP.

    Thank you bro, maybe this was the solution my professor was looking because he told me my code was too long. any other methods tho??

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: I need to create a certain SHAPE with FOR LOOP.

    Actually Kevin the for loop should have the statement like this i<8.So the answer is(basically what Kevin said,only a number changed)
    public static void main(String[] args) {
            for (int i = 0; i < 8; i++) {
                if (i % 2 == 0) {
                    System.out.println(" ************");
                } else {
                    System.out.println("************");
                }
            }
        }

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: I need to create a certain SHAPE with FOR LOOP.

    Quote Originally Posted by Rage1337 View Post
    Thank you bro, maybe this was the solution my professor was looking because he told me my code was too long. any other methods tho??
    Another method based to kevin's answer is this:
    public static void main(String[] args) {
            for (int i = 0; i < 8; i++) {
                if (i % 2 == 0) 
                    System.out.print(" ");           
                System.out.println("************");
            }
        }

  6. #6
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need to create a certain SHAPE with FOR LOOP.

    Quote Originally Posted by Samaras View Post
    Another method based to kevin's answer is this:
    public static void main(String[] args) {
            for (int i = 0; i < 8; i++) {
                if (i % 2 == 0) 
                    System.out.print(" ");           
                System.out.println("************");
            }
        }
    Good One tho ive got some ideas of what you did. anyways your output was the other way around spaces should be in the second 4th 6th and 8th line.

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: I need to create a certain SHAPE with FOR LOOP.

    Ok,you see what has to be changed.And my list and most simple idea is this

    public static void main(String[] args) {
            for (int i = 0; i < 4; i++) {
                System.out.println("************");           
                System.out.println(" ************");
            }
        }

  8. #8
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: I need to create a certain SHAPE with FOR LOOP.

    Everyone contributing to this thread should read the following:
    The Problem with Spoon-feeding
    Original poster, I do hope you fully understand these handouts, as well as fully understand the academic policy of the school you attend (you mention professor, so I assume this is an assignment)

  9. #9
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need to create a certain SHAPE with FOR LOOP.

    I am aware of this acts, that lab exercise is already done , and as far as im concerned i was just asking for alternatives, sorry for that.

Similar Threads

  1. Moving a shape across a panel
    By havinFun in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 17th, 2012, 05:56 PM
  2. Replies: 1
    Last Post: April 30th, 2012, 08:16 AM
  3. Shape error?
    By xTommy24 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 20th, 2011, 08:46 AM
  4. Replies: 5
    Last Post: August 11th, 2011, 12:39 PM
  5. Replies: 1
    Last Post: October 16th, 2010, 03:32 PM