help with 2d array

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

help with 2d array

 
0
  #1
Apr 18th, 2009
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

  1. private void drawHangman(){
  2.  
  3. int rows = 6;
  4. int cols = 6;
  5. String[][] draw = new String[rows][cols];
  6.  
  7. for(int n=1; n<rows; n++){//draw hang pole ||
  8. draw[n][0] = "||";
  9. }
  10.  
  11. for(int r=0; r<cols; r++){//draw hang pole =
  12. draw[0][r]= "=";
  13. }
  14. draw[1][3] = "|";
  15.  
  16. if (wrong >= 1){//draw head
  17. draw[2][3] = "O";
  18. }
  19. if (wrong >= 2){//draw body
  20. draw[3][3] = "|";
  21. }
  22. if (wrong >= 3){//draw hand
  23. draw[3][2] = "/";
  24. }
  25. if (wrong >= 4){//draw hand
  26. draw[3][4] = "\\";
  27. }
  28. if (wrong >= 5){//draw leg
  29. draw[4][2] = "/";
  30. }
  31. if (wrong == 6){//draw leg
  32. draw[4][4] = "\\";
  33. }
  34.  
  35. for(int x = 0; x < rows; x++){
  36. for(int y = 0;y < cols; y++){
  37. System.out.println(draw[x][y]);
  38. }
  39.  
  40. }
  41.  
  42. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,027
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 151
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: help with 2d array

 
0
  #2
Apr 18th, 2009
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 " ");
Last edited by JamesCherrill; Apr 18th, 2009 at 10:13 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 357 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC