| | |
help with 2d array
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
ok so I need to use a 2d array to draw a hangman picture for a hangman game. the picture should be something like this
============
|<space>|
|<space>O
|<space>/|\
|<space>/\
|
|
but when i run this code with main. i get this
=
=
=
=
=
=
||
null
null
|
null
null
||
null
null
O
null
null
||
null
/
|
null
null
||
null
null
null
null
null
||
null
null
null
null
null
============
|<space>|
|<space>O
|<space>/|\
|<space>/\
|
|
but when i run this code with main. i get this
=
=
=
=
=
=
||
null
null
|
null
null
||
null
null
O
null
null
||
null
/
|
null
null
||
null
null
null
null
null
||
null
null
null
null
null
Java Syntax (Toggle Plain Text)
private void drawHangman(){ int rows = 6; int cols = 6; String[][] draw = new String[rows][cols]; for(int n=1; n<rows; n++){//draw hang pole || draw[n][0] = "||"; } for(int r=0; r<cols; r++){//draw hang pole = draw[0][r]= "="; } draw[1][3] = "|"; if (wrong >= 1){//draw head draw[2][3] = "O"; } if (wrong >= 2){//draw body draw[3][3] = "|"; } if (wrong >= 3){//draw hand draw[3][2] = "/"; } if (wrong >= 4){//draw hand draw[3][4] = "\\"; } if (wrong >= 5){//draw leg draw[4][2] = "/"; } if (wrong == 6){//draw leg draw[4][4] = "\\"; } for(int x = 0; x < rows; x++){ for(int y = 0;y < cols; y++){ System.out.println(draw[x][y]); } } }
•
•
Join Date: Apr 2008
Posts: 985
Reputation:
Solved Threads: 146
System.out.println (short for Print Line) puts a newline char at the end whatever it prints, so you get one array element on each line.
System.out.print does the same as System.out.println but does not add the newline, so that's the one you need. You'll then need to do your own newline "\n" at the end of each row.
To get rid of the nulls, you need to ensure that every element contains a valid String (eg " ");
System.out.print does the same as System.out.println but does not add the newline, so that's the one you need. You'll then need to do your own newline "\n" at the end of each row.
To get rid of the nulls, you need to ensure that every element contains a valid String (eg " ");
Last edited by JamesCherrill; Apr 18th, 2009 at 10:13 am.
![]() |
Similar Threads
- Can I ghost a RAID array??? (Windows NT / 2000 / XP)
- Creating dynamic array structures (C++)
- Array limit (C)
- struct dynamic 2d array alloc (C)
- string to integer array transformation (C)
- Array (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Database problem
- Next Thread: Null pointer Exception
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary bluetooth character chat class classes client code component consumer csv database desktop draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie objects online oracle oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion robot rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time tree ubuntu update windows





