• Java for loop help
    6 replies, posted
I just started teaching myself Java last week and I'm having a problem with this for loop. The program is supposed to output an ASCII version of a bomb, and I've already made the fuse and the top half of the bomb, and I'm having trouble making the bottom half of the bomb. Here's the code: [code] public class AsciiArt { public static void main(String[] args) { fuse(); topHalf(); bottomHalf(); } // This outputs the fuse of the bomb public static void fuse () { for (int h = 1; h <= 3; h++) { System.out.print(" "); } for (int i = 1; i <= 2; i++) { System.out.print("*"); } System.out.println(); for (int h = 1; h <= 2; h++) { System.out.print(" "); } for (int a = 1; a <= 4; a++) { System.out.print("*"); } System.out.println(); } // This outputs the top half of the bomb public static void topHalf () { for (int line = 1; line <= 4; line++) { for (int spaces = 1; spaces <= 4 - line; spaces++) { System.out.print(" "); } for (int dashes = 1; dashes <= 2 * line; dashes++) { System.out.print("|"); } System.out.println(); } } // This outputs the bottom half of the bomb public static void bottomHalf() { for (int line = 1; line <4; line++) { for (int spaces = 1; spaces <= line - 1; spaces++) { System.out.print(" "); } for (int dashes = 1; dashes <= 9 - line; dashes++) { System.out.print("|"); } System.out.println(); } } } [/code]The part I just need help with is the very last part of my bottomHalf method, namely the "dashes <= 9 - line;" part. I'm having trouble reversing the expression I used for the top half to make it decrease in the amount of dashes per line. Thanks.
[code]public class Main { public static void main(String[] args) { fuse(); topHalf(); bottomHalf(); } // This outputs the fuse of the bomb public static void fuse () { for (int h = 1; h <= 3; h++) { System.out.print(" "); } for (int i = 1; i <= 2; i++) { System.out.print("*"); } System.out.println(); for (int h = 1; h <= 2; h++) { System.out.print(" "); } for (int a = 1; a <= 4; a++) { System.out.print("*"); } System.out.println(); } // This outputs the top half of the bomb public static void topHalf () { for (int line = 1; line <= 4; line++) { for (int spaces = 1; spaces <= 4 - line; spaces++) { System.out.print(" "); } for (int dashes = 1; dashes <= 2 * line; dashes++) { System.out.print("|"); } System.out.println(); } } // This outputs the bottom half of the bomb public static void bottomHalf() { int lines=3; int spaces=1; for (int line = lines; line >=1; line--) { for (int space = 1;space<=spaces;space++) { System.out.print(" "); } spaces++; for (int dashes = 2*line; dashes >=1; dashes--) { System.out.print("|"); } System.out.println(); } } }[/code] [editline]15th January 2011[/editline] What I did is: add an integer "lines" which lets you define the number of lines (this isn't necessary but it made it a little more clear what I was doing) added an integer called spaces which should always start at 1. This is the number of spaces to output on the next line. the spaces for loop does this: current space = 1 print a space until current space = the number of spaces to output end spaces for loop then the program adds one to the number of spaces output on the next line. the dashes for loop does this: dashes = 2*line (your formula for the top half) print a | sign until dashes is less than 1 (no more dashes to output) end dashes loop the main loop sets the current line to the max number of lines and keeps looping until there are no more lines left to print.
Thanks for the help.
Why are you creating all of these loop variables in fuse? Just create i at the top and set it for each for loop. [code]public static void fuse () { int i; for (i = 1; i <= 3; i++) { System.out.print(" "); } for (i = 1; i <= 2; i++) { System.out.print("*"); } System.out.println(); for (i = 1; i <= 2; i++) { System.out.print(" "); } for (i = 1; i <= 4; i++) { System.out.print("*"); } System.out.println(); }[/code]
[QUOTE=Pepin;27447715]Why are you creating all of these loop variables in fuse? Just create i at the top and set it for each for loop.[/QUOTE] It's a matter of preference, but I agree with the way the OP declared those variables. If the variable is only needed within the context of the loop, its scope should be limited to the code within the loop.
Also, there is some redundancy with that function. The only difference with any of those for loops is the number you're comparing against, so you could use another for loop and just loop through that. [code]public static void fuse() { int i; int j; int[] numHolder = { 3, 2, 2, 4 }; for (i = 0; i < 4; i++) { for (j = 1; j <= numHolder[i]; j++) { if (i % 2) { System.out.print(" "); } else { System.out.print("*"); } } System.out.println(); }[/code]Pretty sure that will work. Unless 0 doesn't evaluate to false like it does in C. [QUOTE=Wyzard;27447764]It's a matter of preference, but I agree with the way the OP declared those variables. If the variable is only needed within the context of the loop, its scope should be limited to the code within the loop.[/QUOTE] I don't know about that in this case though because there isn't any reason to initialize the variable over and over again. I'm pretty sure it'd be more efficient to use one variable declared at the start of the function. I'll admit that any gains would very very minimal and it really doesn't matter, but I think it'd make more sense to do it that way in this case.
[QUOTE=Pepin;27448014]I don't know about that in this case though because there isn't any reason to initialize the variable over and over again.[/QUOTE] Both ways involve assigning the value 1 to the variable at the start of the loop. There isn't really any extra work involved in "creating" the variable, at runtime, aside from assigning it a value. It's likely that the compiler will recognize that the variables' scopes don't overlap, and assign them all to the same slot in the compiled method's local variable table, just like what you'd get from the single-variable approach.
Sorry, you need to Log In to post a reply to this thread.