d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Java(eclipse) Issue. Writing Method > To Print A Box(x,y Parameters)
Add Reply New Topic New Poll
Member
Posts: 2,595
Joined: Feb 7 2007
Gold: Locked
Trader: Scammer
Warn: 30%
Nov 3 2013 06:56pm
Hi there. I'm trying to figure out how to write a method that will allow me to type:
rectangleBody(9,10);

and have the console print out a rectangle 9 wide, 10 tall.

basically if i put in

rectangleBody(5,5);

i want to see:
#####
#####
#####
#####
#####

I have thus far not had any luck with this.
Any help would be appreciated
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 3 2013 08:38pm
void rectangleBody(int width, int height){
// add your logic here. to print to console, use:
System.out.print("x");
}
Member
Posts: 2,595
Joined: Feb 7 2007
Gold: Locked
Trader: Scammer
Warn: 30%
Nov 3 2013 08:54pm
Quote (carteblanche @ Nov 3 2013 10:38pm)
void rectangleBody(int width, int height){
// add your logic here. to print to console, use:
System.out.print("x");
}


My issue i've been having is the actual logic itself. I've got to the point where if i do rectangleBody(9, 10); i get 90 # symbols going either in a single line or 1 per line covering 90 lines.

I appreciate your answer, but my problem is that i cant figure out the logic needed to make rectangleBody(9,10) give me 10 lines each with 9 # symbols in it.
I've done some searching on methods with multiple parameters, and read through a java textbook i have but cant seem to figure this code out =(
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 3 2013 08:59pm
Quote (amahumahaba @ Nov 3 2013 10:54pm)
My issue i've been having is the actual logic itself. I've got to the point where if i do rectangleBody(9, 10); i get 90 # symbols going either in a single line or 1 per line covering 90 lines.

I appreciate your answer, but my problem is that i cant figure out the logic needed to make rectangleBody(9,10) give me 10 lines each with 9 # symbols in it.
I've done some searching on methods with multiple parameters, and read through a java textbook i have but cant seem to figure this code out =(


you should've included your code in the original post.

you're gonna need 2 loops, one to handle the rows and one to handle the columns. something similar to:

Code
for row = 1 to number_rows{
for column = 1 to number_columns{
// print the # symbol with quantity number_columns
}
// we completed a single line. now move to next line
}


This post was edited by carteblanche on Nov 3 2013 09:05pm
Member
Posts: 2,595
Joined: Feb 7 2007
Gold: Locked
Trader: Scammer
Warn: 30%
Nov 3 2013 09:02pm
Quote (carteblanche @ Nov 3 2013 10:59pm)
you should've included your code in the original post.

you're gonna need 2 loops, one to handle the rows and one to handle the columns. something similar to:

Code
for row = 1 to number_rows{
  for column = 1 to number_columns{
      // print the # symbol with quantity number_columns
      print #
  }
  // we completed a single line. now move to next line
  print \n
}


Thankyou so much. pretty sure this is exactly what i needed. Just something to let me know what i am really trying to do(as lame as trying to figure out what i am trying to do sounds haha =p)

I apologize if i made this unintentionally overcomplicated, i'm pretty new to asking for help on here, but i really really appreciate your help =)
Member
Posts: 1,081
Joined: Aug 25 2013
Gold: Locked
Trader: Scammer
Nov 4 2013 04:37pm
make a 2 dimensional array

Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Nov 4 2013 06:36pm
Quote (GetSpanked @ Nov 4 2013 05:37pm)
make a 2 dimensional array


Why would you use a 2D array for this?
Member
Posts: 4
Joined: Nov 6 2013
Gold: 0.00
Nov 11 2013 01:17am
Quote (GetSpanked @ Nov 5 2013 04:07am)

make a 2 dimensional array


Why 2D array? This can be done with for loop in other for loop. What is the reason to increase the complexity by adding the array even nested loop will also be there?
Member
Posts: 4
Joined: Nov 6 2013
Gold: 0.00
Nov 26 2013 11:22pm
Quote
Why 2D array? This can be done with for loop in other for loop. What is the reason to increase the complexity by adding the array even nested loop will also be there?


You are right but loops are to be used in both the cases in the same way then what is the complexity here? If you use array you can have different values at every position.
Code

String a[][] = new String[5][5];
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}


one can change the data type with ones need and if one wants to have same value at every position than it should be done without array.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll