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

• 09-21-2008, 12:03 AM
carlodelmundo
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:

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.
• 09-21-2008, 12:10 AM
Fubarable
Try thinking about nested for loops: one for-loop inside of another.
• 09-21-2008, 12:30 AM
carlodelmundo
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.
• 09-21-2008, 12:35 AM
Fubarable
You could also use the System.out.print(...) method and a for loop to print something like this:
-----
• 09-21-2008, 01:39 AM
serjant
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();         } }```
• 09-21-2008, 02:41 AM
Norm
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.
• 09-21-2008, 04:17 AM
Fubarable
Quote:

Originally Posted by serjant
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?
• 09-21-2008, 11:20 AM
serjant
Fubarable,you are completely right