Results 1 to 8 of 8
- 09-21-2008, 12:03 AM #1
Member
- Join Date
- Aug 2008
- Posts
- 31
- Rep Power
- 0
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:
Give me some tips (not code) in the right direction please--I'm still slightly confused.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); } } }
-
Try thinking about nested for loops: one for-loop inside of another.
- 09-21-2008, 12:30 AM #3
Member
- Join Date
- Aug 2008
- Posts
- 31
- Rep Power
- 0
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.
-
You could also use the System.out.print(...) method and a for loop to print something like this:
-----
- 09-21-2008, 01:39 AM #5
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(); } }
- 09-21-2008, 02:41 AM #6
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.
-
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 #8
Similar Threads
-
Hwlp with "Open", "Save", "Save as..."
By trill in forum New To JavaReplies: 3Last Post: 11-02-2010, 09:26 AM -
<core:forEach var="" begin="+<%=j%>+">???
By freddieMaize in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-27-2008, 01:20 AM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM -
Exception in thread "main" java.net.ConnectException: Connection timed out
By osval in forum Advanced JavaReplies: 1Last Post: 07-27-2007, 10:59 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks