Results 1 to 8 of 8
  1. #1
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default Using for loops to create a "bridge" made out of hyphens

    Hi.

    *----
    -*---
    --*--
    ---*-
    ----*

    I need to create an algorithm that will create "a man on a bridge" in which the man is denoted by the * (asterisk), and the bridge width is denoted by - (hyphens).

    I have no problem creating in creating the man moving such that :

    *
    **
    ***
    ****

    The code for above is:

    Java Code:
    public class ForLoopsTest
    {
    	public static void main(String[] args)
    	{
    		String r ="";
    		final int WIDTH = 4;	
    		for (int i = 1; i <= WIDTH; i++)
    		{
    			r = r + "*";
    			System.out.println(r);		
    		}	
    	}
    }
    Give me some tips (not code) in the right direction please--I'm still slightly confused.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Try thinking about nested for loops: one for-loop inside of another.

  3. #3
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default

    I understand the concept of for loops, but how can I apply this to my current problem?

    I think the first step for me... is to use nested loops to output

    *
    *
    *
    *

    I know I have to concatenate a space with the asterisk... but again, just too confused to start.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You could also use the System.out.print(...) method and a for loop to print something like this:
    -----

  5. #5
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Java Code:
    
    public class Bridge {
    
    	public Bridge(){
    		// TODO Auto-generated method stub
    		for(int i=0;i<=4;i++){
    		       for(int j=0;j<=4;j++){
    		            if(j==i){
    		               System.out.print("*");
    		              
    		            }
    		            else{System.out.print("_");}
    		       }
    		       System.out.println();
    		}
    	}
    	
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		new Bridge();
    	}
    
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Looking at your first post, it looks like you output 5 characters on every row. The position of the * changes for each row. Its position in the row = the row number.
    Create a String with 5 hyphens. In the loop replace the char at the index equal to the row number with an *. Use a StringBuffer for ease of doing the replace.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by serjant View Post
    Java Code:
    public class Bridge {
     // ...
    }
    serjant, I'm as impressed as the next bloke with your amazing and awesome coding abilities, but the original poster requested (and rightfully so) hints not code. It's the old parable of give a man a fish vs teach him to fish. It would be easy for any of us to spit out out code like this, far easier than leading the OP towards the solution, but is it the right thing to do?

  8. #8
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 09:26 AM
  2. <core:forEach var="" begin="+<%=j%>+">???
    By freddieMaize in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2008, 01:20 AM
  3. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 AM
  4. Replies: 1
    Last Post: 07-27-2007, 10:59 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

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