Ste Posted April 13, 2005 Posted April 13, 2005 (edited) /* Steven Borchard Diamond*/ public class TrivialApplication { public static void main(String args[]) { Edited April 13, 2005 by Ste Share this post Link to post Share on other sites More sharing options...
markiemrboo Posted April 13, 2005 Posted April 13, 2005 Try putting it in a code block, should come out right then. Share this post Link to post Share on other sites More sharing options...
LobbDogg Posted April 13, 2005 Posted April 13, 2005 I remember doing that project for one of my classes in college. yet I have no idea how I did it anymore. Share this post Link to post Share on other sites More sharing options...
markiemrboo Posted April 13, 2005 Posted April 13, 2005 I remember doing that project for one of my classes in college. yet I have no idea how I did it anymore. 461814[/snapback] I had to do something about this in my first year uni programming unit EXAM Share this post Link to post Share on other sites More sharing options...
Ste Posted April 14, 2005 Posted April 14, 2005 So uh Like I said any suggestions? Share this post Link to post Share on other sites More sharing options...
markiemrboo Posted April 14, 2005 Posted April 14, 2005 public class Diamond { public static void main(String[] args) { int size = 8; int evenstart = 3; // top half for (int i = size; i > 0; i -= 2) { // spaces left side for (int j = 0; j < Math.floor(i / 2); j++) { System.out.print("-"); } // middle stars for (int j = i; j <= size; j++) { System.out.print("*"); } System.out.println(); } // bottom half // fix up even numbers if (size % 2 == 0) evenstart = 0; for (int i = evenstart; i <= size; i += 2) { // spaces left side for (int j = 0; j < Math.floor(i / 2); j++) { System.out.print("-"); } // middle stars for (int j = i; j <= size; j++) { System.out.print("*"); } System.out.println(); } } } Bit hacky but works. EDIT: eek, you could also do it the way you were doing it like so... public class Diamond { public static void main(String[] args) { int size = 9; String A="*"; for(int x=0;x<size;x++){ for (int i = x; i < size; i++) { System.out.print(" "); } System.out.println(A); A += "**"; } A="*"; for(int x = 0; x < (size + 1); x++) { A += "**"; } for(int x=0;x<(size + 1);x++){ for (int i = x; i > 0; i--) { System.out.print(" "); } A = A.substring(2, A.length()); System.out.println(A); } } } Share this post Link to post Share on other sites More sharing options...
Ste Posted April 14, 2005 Posted April 14, 2005 (edited) Ok its works but the diamond is to big. its is 10 rows to big, but thats ok Ill figure it out latter, though explain to me the use of the last part of the code, I have never encountered anything relating to substring. The next part of my program, is to modify that code so a user can enter an odd number 1 - 19 to specify the numbers of rows. Edited April 14, 2005 by Ste Share this post Link to post Share on other sites More sharing options...
LobbDogg Posted April 14, 2005 Posted April 14, 2005 Ok when I did mine, I had to use recursion to make it, it was one of the requirements, I don't know if you need to make it a certain way or not but recursion should work. Share this post Link to post Share on other sites More sharing options...
markiemrboo Posted April 14, 2005 Posted April 14, 2005 Ok its works but the diamond is to big. its is 10 rows to big, but thats ok Ill figure it out latter, though explain to me the use of the last part of the code, I have never encountered anything relating to substring. The next part of my program, is to modify that code so a user can enter an odd number 1 - 19 to specify the numbers of rows. 462343[/snapback] If you need user input number of rows, notice the 'size' variable? Use that. Since what I have basically done is drawn a top triangle, the middle line and then an upside down button triangle, if size = 2 then you get (2 * 2) + 1 rows. This would mean if the user said they wanted, say, 5 rows you would have to set size = (5 /2) .. which actually equals 1.5, but it'll round down to 1 as it is an int, and as you can see (2*2) + 1 = 5 rows substring, gets a portion of the string. You can specift where you want to start and where you want to end. It's basically chopping off the first two stars each time, although you could have very easily chopped off the last two stars by starting from the beginning of the string substring(0, xxx) and returning the portion of the string up until the length() - 2, so substring(0, str.length() - 2); Did any of that make any sense to you, at all? Share this post Link to post Share on other sites More sharing options...
Ste Posted April 14, 2005 Posted April 14, 2005 Yes But we haven't learned substrings yet in fact, that command is learned in programming 2. So I can't use it. Share this post Link to post Share on other sites More sharing options...
markiemrboo Posted April 14, 2005 Posted April 14, 2005 I love it when teachers decide you haven't been taught it so you are not allowed to do it that way. Such bullcrap. Thankfully my Uni doesn't care for silly little thing like that, in fact I think they probably like the fact that you have gone away, read some docs, and figured out how to use a new function/whatever! Well anyway, if you aint learnt it yet you will have to go with my first solution... or wait for someone else to come up with a better way than that (no doubt there is one, but hell if I can figure out any other way) Share this post Link to post Share on other sites More sharing options...
Ste Posted April 14, 2005 Posted April 14, 2005 (edited) OK thanks, first big thing I had trouble. Whats math.floor Edited April 15, 2005 by Ste Share this post Link to post Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now