Loop Exercise: Printing a tree on console using for loops (Stuck on details)
This is an exercise for uni I've been working on but I've been stuck on just one last little problem with the output and I just can't find out why.
Goal:
User inputs height of tree. It gets printed in the following format:
E.g height 4
4| * |5=2+1+2 (width = 2 spaces+ 1 char + 2 spaces)
3| *** |5=1+3+1
2|*****|5=0+5+0
1| *** |5=1+3+1
Note: The Root is always 3 characters long and should be 1/4th of the height (round off)
Here's my problem: It works fine for every input <8. First I was looking at the wrong place because I feel like it had something to do with the number itself. But 8 is the first height where the root is 2 lines instead of one. (8 * 1/4 = 2) Regardless I could not find a solution (not even a bad one).
I hope my code is readable, I tried to remove everything unnecessary and change the variables to English ones.
My assignment is basically done, it's just this minor detail I cant get a grip on. It's not like I'm asking anyone to do my homework for me; there's on learning in that I agree.
Here's my output for height (works fine)
7| * |11=5+1+5
6| *** |11=4+3+4
5| ***** |11=3+5+3
4| ******* |11=2+7+2
3| ********* |11=1+9+1
2|***********|11=0+11+0
1| *** |11=4+3+4
For anything bigger than 8, e.G 8 I get the following problem in the Root Lines of the tree:
8| * |11=5+1+5
7| *** |11=4+3+4
6| ***** |11=3+5+3
5| ******* |11=2+7+2
4| ********* |11=1+9+1
3| *********** |11=0+11+0
2| *** |11=4+3+4 <--- amount of spaces is still correct but my formating got butchered as you can see, line 3 no longer touches the edges (|), its shifted
1| *** |11=4+3+4
Here's my code:
import java.util.Scanner;
public class SchleifenTestat
public static void main(String args)
Scanner read = new Scanner(System.in);
System.out.print("Enter height:");
int height= read.nextInt();
int heightOutput= height;
int root = (int)(height*0.25F);
int width= ((height- root) * 2) -1; //Amount of chars in the widest tree line
int spaces = (width- 1); //Amount of spaces line 1
int charCount = 1;
int tree = (height- root);
for(int i = 0; i < tree; i++)
");
heightOutput--;
for(int j=0; j<= height- i -3 ;j++)
System.out.print(" ");
for(int k=0; k < i*2 + 1; k++ )
System.out.print("*");
for(int j=0; j<=(height- i - 3);j++) //5 = 2 + * + 2
System.out.print(" ");
System.out.print("
for(int i = heightOutput; heightOutput> 0; heightOutput--)
");
for(int j = 0; j<((width-3)/2);j++)
System.out.print(" "); //Spaces left side
System.out.print("***"); //Root
for(int j = 0; j<((width-3)/2);j++)
System.out.print(" "); //Spaces right side
System.out.print("
java loops for-loop console-application println
|
show 2 more comments
This is an exercise for uni I've been working on but I've been stuck on just one last little problem with the output and I just can't find out why.
Goal:
User inputs height of tree. It gets printed in the following format:
E.g height 4
4| * |5=2+1+2 (width = 2 spaces+ 1 char + 2 spaces)
3| *** |5=1+3+1
2|*****|5=0+5+0
1| *** |5=1+3+1
Note: The Root is always 3 characters long and should be 1/4th of the height (round off)
Here's my problem: It works fine for every input <8. First I was looking at the wrong place because I feel like it had something to do with the number itself. But 8 is the first height where the root is 2 lines instead of one. (8 * 1/4 = 2) Regardless I could not find a solution (not even a bad one).
I hope my code is readable, I tried to remove everything unnecessary and change the variables to English ones.
My assignment is basically done, it's just this minor detail I cant get a grip on. It's not like I'm asking anyone to do my homework for me; there's on learning in that I agree.
Here's my output for height (works fine)
7| * |11=5+1+5
6| *** |11=4+3+4
5| ***** |11=3+5+3
4| ******* |11=2+7+2
3| ********* |11=1+9+1
2|***********|11=0+11+0
1| *** |11=4+3+4
For anything bigger than 8, e.G 8 I get the following problem in the Root Lines of the tree:
8| * |11=5+1+5
7| *** |11=4+3+4
6| ***** |11=3+5+3
5| ******* |11=2+7+2
4| ********* |11=1+9+1
3| *********** |11=0+11+0
2| *** |11=4+3+4 <--- amount of spaces is still correct but my formating got butchered as you can see, line 3 no longer touches the edges (|), its shifted
1| *** |11=4+3+4
Here's my code:
import java.util.Scanner;
public class SchleifenTestat
public static void main(String args)
Scanner read = new Scanner(System.in);
System.out.print("Enter height:");
int height= read.nextInt();
int heightOutput= height;
int root = (int)(height*0.25F);
int width= ((height- root) * 2) -1; //Amount of chars in the widest tree line
int spaces = (width- 1); //Amount of spaces line 1
int charCount = 1;
int tree = (height- root);
for(int i = 0; i < tree; i++)
");
heightOutput--;
for(int j=0; j<= height- i -3 ;j++)
System.out.print(" ");
for(int k=0; k < i*2 + 1; k++ )
System.out.print("*");
for(int j=0; j<=(height- i - 3);j++) //5 = 2 + * + 2
System.out.print(" ");
System.out.print("
for(int i = heightOutput; heightOutput> 0; heightOutput--)
");
for(int j = 0; j<((width-3)/2);j++)
System.out.print(" "); //Spaces left side
System.out.print("***"); //Root
for(int j = 0; j<((width-3)/2);j++)
System.out.print(" "); //Spaces right side
System.out.print("
java loops for-loop console-application println
What are hoehe and stamm?
– Ralf Renz
Nov 13 '18 at 13:02
Have you tried a debugger? I recommend: perform your algorithm by hand with pen and paper for the smallest example that fails.
– MrSmith42
Nov 13 '18 at 13:03
@RalfRenz "Hoehe" (German word "Höhe", alternatively spelled due to no 'ö' in Java) is the height of something, the one of the tree most likely. "Stamm" means the trunk of the tree.
– deHaar
Nov 13 '18 at 13:04
@qdeHaar: It's totally legal to use 'ö' but I would not recommend it.
– MrSmith42
Nov 13 '18 at 13:06
Im sorry guys. I thought I exchanged all my variables for english names! I forgot some. @deHaar cleared it up, thanks for that. MrSmith42 I thought that aswell. I recently tried out a new editor (Atom) and it wont let me use 'ö'. As you said it shouldnt be used regardless. I should use english names from the getgo anyways, just a bad habit of mine. Also I will try going through it with a pen
– sonti yo
Nov 13 '18 at 13:07
|
show 2 more comments
This is an exercise for uni I've been working on but I've been stuck on just one last little problem with the output and I just can't find out why.
Goal:
User inputs height of tree. It gets printed in the following format:
E.g height 4
4| * |5=2+1+2 (width = 2 spaces+ 1 char + 2 spaces)
3| *** |5=1+3+1
2|*****|5=0+5+0
1| *** |5=1+3+1
Note: The Root is always 3 characters long and should be 1/4th of the height (round off)
Here's my problem: It works fine for every input <8. First I was looking at the wrong place because I feel like it had something to do with the number itself. But 8 is the first height where the root is 2 lines instead of one. (8 * 1/4 = 2) Regardless I could not find a solution (not even a bad one).
I hope my code is readable, I tried to remove everything unnecessary and change the variables to English ones.
My assignment is basically done, it's just this minor detail I cant get a grip on. It's not like I'm asking anyone to do my homework for me; there's on learning in that I agree.
Here's my output for height (works fine)
7| * |11=5+1+5
6| *** |11=4+3+4
5| ***** |11=3+5+3
4| ******* |11=2+7+2
3| ********* |11=1+9+1
2|***********|11=0+11+0
1| *** |11=4+3+4
For anything bigger than 8, e.G 8 I get the following problem in the Root Lines of the tree:
8| * |11=5+1+5
7| *** |11=4+3+4
6| ***** |11=3+5+3
5| ******* |11=2+7+2
4| ********* |11=1+9+1
3| *********** |11=0+11+0
2| *** |11=4+3+4 <--- amount of spaces is still correct but my formating got butchered as you can see, line 3 no longer touches the edges (|), its shifted
1| *** |11=4+3+4
Here's my code:
import java.util.Scanner;
public class SchleifenTestat
public static void main(String args)
Scanner read = new Scanner(System.in);
System.out.print("Enter height:");
int height= read.nextInt();
int heightOutput= height;
int root = (int)(height*0.25F);
int width= ((height- root) * 2) -1; //Amount of chars in the widest tree line
int spaces = (width- 1); //Amount of spaces line 1
int charCount = 1;
int tree = (height- root);
for(int i = 0; i < tree; i++)
");
heightOutput--;
for(int j=0; j<= height- i -3 ;j++)
System.out.print(" ");
for(int k=0; k < i*2 + 1; k++ )
System.out.print("*");
for(int j=0; j<=(height- i - 3);j++) //5 = 2 + * + 2
System.out.print(" ");
System.out.print("
for(int i = heightOutput; heightOutput> 0; heightOutput--)
");
for(int j = 0; j<((width-3)/2);j++)
System.out.print(" "); //Spaces left side
System.out.print("***"); //Root
for(int j = 0; j<((width-3)/2);j++)
System.out.print(" "); //Spaces right side
System.out.print("
java loops for-loop console-application println
This is an exercise for uni I've been working on but I've been stuck on just one last little problem with the output and I just can't find out why.
Goal:
User inputs height of tree. It gets printed in the following format:
E.g height 4
4| * |5=2+1+2 (width = 2 spaces+ 1 char + 2 spaces)
3| *** |5=1+3+1
2|*****|5=0+5+0
1| *** |5=1+3+1
Note: The Root is always 3 characters long and should be 1/4th of the height (round off)
Here's my problem: It works fine for every input <8. First I was looking at the wrong place because I feel like it had something to do with the number itself. But 8 is the first height where the root is 2 lines instead of one. (8 * 1/4 = 2) Regardless I could not find a solution (not even a bad one).
I hope my code is readable, I tried to remove everything unnecessary and change the variables to English ones.
My assignment is basically done, it's just this minor detail I cant get a grip on. It's not like I'm asking anyone to do my homework for me; there's on learning in that I agree.
Here's my output for height (works fine)
7| * |11=5+1+5
6| *** |11=4+3+4
5| ***** |11=3+5+3
4| ******* |11=2+7+2
3| ********* |11=1+9+1
2|***********|11=0+11+0
1| *** |11=4+3+4
For anything bigger than 8, e.G 8 I get the following problem in the Root Lines of the tree:
8| * |11=5+1+5
7| *** |11=4+3+4
6| ***** |11=3+5+3
5| ******* |11=2+7+2
4| ********* |11=1+9+1
3| *********** |11=0+11+0
2| *** |11=4+3+4 <--- amount of spaces is still correct but my formating got butchered as you can see, line 3 no longer touches the edges (|), its shifted
1| *** |11=4+3+4
Here's my code:
import java.util.Scanner;
public class SchleifenTestat
public static void main(String args)
Scanner read = new Scanner(System.in);
System.out.print("Enter height:");
int height= read.nextInt();
int heightOutput= height;
int root = (int)(height*0.25F);
int width= ((height- root) * 2) -1; //Amount of chars in the widest tree line
int spaces = (width- 1); //Amount of spaces line 1
int charCount = 1;
int tree = (height- root);
for(int i = 0; i < tree; i++)
");
heightOutput--;
for(int j=0; j<= height- i -3 ;j++)
System.out.print(" ");
for(int k=0; k < i*2 + 1; k++ )
System.out.print("*");
for(int j=0; j<=(height- i - 3);j++) //5 = 2 + * + 2
System.out.print(" ");
System.out.print("
for(int i = heightOutput; heightOutput> 0; heightOutput--)
");
for(int j = 0; j<((width-3)/2);j++)
System.out.print(" "); //Spaces left side
System.out.print("***"); //Root
for(int j = 0; j<((width-3)/2);j++)
System.out.print(" "); //Spaces right side
System.out.print("
java loops for-loop console-application println
java loops for-loop console-application println
edited Nov 14 '18 at 20:20
halfer
14.6k758112
14.6k758112
asked Nov 13 '18 at 12:57
sonti yosonti yo
206
206
What are hoehe and stamm?
– Ralf Renz
Nov 13 '18 at 13:02
Have you tried a debugger? I recommend: perform your algorithm by hand with pen and paper for the smallest example that fails.
– MrSmith42
Nov 13 '18 at 13:03
@RalfRenz "Hoehe" (German word "Höhe", alternatively spelled due to no 'ö' in Java) is the height of something, the one of the tree most likely. "Stamm" means the trunk of the tree.
– deHaar
Nov 13 '18 at 13:04
@qdeHaar: It's totally legal to use 'ö' but I would not recommend it.
– MrSmith42
Nov 13 '18 at 13:06
Im sorry guys. I thought I exchanged all my variables for english names! I forgot some. @deHaar cleared it up, thanks for that. MrSmith42 I thought that aswell. I recently tried out a new editor (Atom) and it wont let me use 'ö'. As you said it shouldnt be used regardless. I should use english names from the getgo anyways, just a bad habit of mine. Also I will try going through it with a pen
– sonti yo
Nov 13 '18 at 13:07
|
show 2 more comments
What are hoehe and stamm?
– Ralf Renz
Nov 13 '18 at 13:02
Have you tried a debugger? I recommend: perform your algorithm by hand with pen and paper for the smallest example that fails.
– MrSmith42
Nov 13 '18 at 13:03
@RalfRenz "Hoehe" (German word "Höhe", alternatively spelled due to no 'ö' in Java) is the height of something, the one of the tree most likely. "Stamm" means the trunk of the tree.
– deHaar
Nov 13 '18 at 13:04
@qdeHaar: It's totally legal to use 'ö' but I would not recommend it.
– MrSmith42
Nov 13 '18 at 13:06
Im sorry guys. I thought I exchanged all my variables for english names! I forgot some. @deHaar cleared it up, thanks for that. MrSmith42 I thought that aswell. I recently tried out a new editor (Atom) and it wont let me use 'ö'. As you said it shouldnt be used regardless. I should use english names from the getgo anyways, just a bad habit of mine. Also I will try going through it with a pen
– sonti yo
Nov 13 '18 at 13:07
What are hoehe and stamm?
– Ralf Renz
Nov 13 '18 at 13:02
What are hoehe and stamm?
– Ralf Renz
Nov 13 '18 at 13:02
Have you tried a debugger? I recommend: perform your algorithm by hand with pen and paper for the smallest example that fails.
– MrSmith42
Nov 13 '18 at 13:03
Have you tried a debugger? I recommend: perform your algorithm by hand with pen and paper for the smallest example that fails.
– MrSmith42
Nov 13 '18 at 13:03
@RalfRenz "Hoehe" (German word "Höhe", alternatively spelled due to no 'ö' in Java) is the height of something, the one of the tree most likely. "Stamm" means the trunk of the tree.
– deHaar
Nov 13 '18 at 13:04
@RalfRenz "Hoehe" (German word "Höhe", alternatively spelled due to no 'ö' in Java) is the height of something, the one of the tree most likely. "Stamm" means the trunk of the tree.
– deHaar
Nov 13 '18 at 13:04
@qdeHaar: It's totally legal to use 'ö' but I would not recommend it.
– MrSmith42
Nov 13 '18 at 13:06
@qdeHaar: It's totally legal to use 'ö' but I would not recommend it.
– MrSmith42
Nov 13 '18 at 13:06
Im sorry guys. I thought I exchanged all my variables for english names! I forgot some. @deHaar cleared it up, thanks for that. MrSmith42 I thought that aswell. I recently tried out a new editor (Atom) and it wont let me use 'ö'. As you said it shouldnt be used regardless. I should use english names from the getgo anyways, just a bad habit of mine. Also I will try going through it with a pen
– sonti yo
Nov 13 '18 at 13:07
Im sorry guys. I thought I exchanged all my variables for english names! I forgot some. @deHaar cleared it up, thanks for that. MrSmith42 I thought that aswell. I recently tried out a new editor (Atom) and it wont let me use 'ö'. As you said it shouldnt be used regardless. I should use english names from the getgo anyways, just a bad habit of mine. Also I will try going through it with a pen
– sonti yo
Nov 13 '18 at 13:07
|
show 2 more comments
1 Answer
1
active
oldest
votes
There is a lot going on here. I took the liberty of refactoring your program a bit/lot. Each time you use the same expression multiple times like (width-3)/2 think about what this value means to you and pass it into a variable with a name.
Same with Lines you use multiple times, like your left and right borders and so on.
If you do this your program should become less numbers and more semantic constructs with meaning you can understand. This is where I arrived at and voila it works. :D
import java.util.Scanner;
public class SchleifenTestat
public static void main(String args)
Scanner read = new Scanner(System.in);
System.out.print("Enter height:");
int height= read.nextInt();
int heightOutput= height;
int root = (int)(height*0.25F);
int width= ((height- root) * 2) -1; //Amount of chars in the widest tree line
int spaces = (width- 1); //Amount of spaces line 1
int spacesOneSide = spaces / 2;
int charCount = 1;
int tree = (height- root);
for(int i = 0; i < tree; i++)
printLeftBorder(heightOutput);
heightOutput--;
printLine(spacesOneSide, charCount);
printRightBorder(width, charCount, spacesOneSide);
charCount+=2;
spacesOneSide--;
while(heightOutput> 0)
int freeSpacesEachSide = ((width-3) / 2);
printLeftBorder(heightOutput);
printLine(freeSpacesEachSide, 3);
printRightBorder(width, 3, freeSpacesEachSide);
heightOutput--;
read.close();
private static void printRightBorder(int width, int charCount, int spacesOneSide)
System.out.print("
private static void printLeftBorder(int number)
");
private static void printLine(int spacesOneSide, int charCount)
printString(" ", spacesOneSide);
printString("*", charCount);
printString(" ", spacesOneSide);
private static void printString(String string, int times)
for(int i = 0; i < times; i++)
System.out.print(string);
Oh and close your scanners. If you have any questions regard my answer, feel free to ask, slow day :)
Happy Coding
First of all thanks for the effort. Your code is understandable! I think I got too hung up on small problems and forgot to focus on proper code structure doing so. I feel like I would have had a much easier time finding logical errors this way. And of course, I should close my Scanner!!! Thanks for the help
– sonti yo
Nov 13 '18 at 14:11
That's usually the way it goes. When you are stuck chasing small errors, take a step back and restructure your code. Either it's a stupid -1 you are missing or your structure is needlessly complicated.
– user10419911
Nov 13 '18 at 14:47
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281534%2floop-exercise-printing-a-tree-on-console-using-for-loops-stuck-on-details%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is a lot going on here. I took the liberty of refactoring your program a bit/lot. Each time you use the same expression multiple times like (width-3)/2 think about what this value means to you and pass it into a variable with a name.
Same with Lines you use multiple times, like your left and right borders and so on.
If you do this your program should become less numbers and more semantic constructs with meaning you can understand. This is where I arrived at and voila it works. :D
import java.util.Scanner;
public class SchleifenTestat
public static void main(String args)
Scanner read = new Scanner(System.in);
System.out.print("Enter height:");
int height= read.nextInt();
int heightOutput= height;
int root = (int)(height*0.25F);
int width= ((height- root) * 2) -1; //Amount of chars in the widest tree line
int spaces = (width- 1); //Amount of spaces line 1
int spacesOneSide = spaces / 2;
int charCount = 1;
int tree = (height- root);
for(int i = 0; i < tree; i++)
printLeftBorder(heightOutput);
heightOutput--;
printLine(spacesOneSide, charCount);
printRightBorder(width, charCount, spacesOneSide);
charCount+=2;
spacesOneSide--;
while(heightOutput> 0)
int freeSpacesEachSide = ((width-3) / 2);
printLeftBorder(heightOutput);
printLine(freeSpacesEachSide, 3);
printRightBorder(width, 3, freeSpacesEachSide);
heightOutput--;
read.close();
private static void printRightBorder(int width, int charCount, int spacesOneSide)
System.out.print("
private static void printLeftBorder(int number)
");
private static void printLine(int spacesOneSide, int charCount)
printString(" ", spacesOneSide);
printString("*", charCount);
printString(" ", spacesOneSide);
private static void printString(String string, int times)
for(int i = 0; i < times; i++)
System.out.print(string);
Oh and close your scanners. If you have any questions regard my answer, feel free to ask, slow day :)
Happy Coding
First of all thanks for the effort. Your code is understandable! I think I got too hung up on small problems and forgot to focus on proper code structure doing so. I feel like I would have had a much easier time finding logical errors this way. And of course, I should close my Scanner!!! Thanks for the help
– sonti yo
Nov 13 '18 at 14:11
That's usually the way it goes. When you are stuck chasing small errors, take a step back and restructure your code. Either it's a stupid -1 you are missing or your structure is needlessly complicated.
– user10419911
Nov 13 '18 at 14:47
add a comment |
There is a lot going on here. I took the liberty of refactoring your program a bit/lot. Each time you use the same expression multiple times like (width-3)/2 think about what this value means to you and pass it into a variable with a name.
Same with Lines you use multiple times, like your left and right borders and so on.
If you do this your program should become less numbers and more semantic constructs with meaning you can understand. This is where I arrived at and voila it works. :D
import java.util.Scanner;
public class SchleifenTestat
public static void main(String args)
Scanner read = new Scanner(System.in);
System.out.print("Enter height:");
int height= read.nextInt();
int heightOutput= height;
int root = (int)(height*0.25F);
int width= ((height- root) * 2) -1; //Amount of chars in the widest tree line
int spaces = (width- 1); //Amount of spaces line 1
int spacesOneSide = spaces / 2;
int charCount = 1;
int tree = (height- root);
for(int i = 0; i < tree; i++)
printLeftBorder(heightOutput);
heightOutput--;
printLine(spacesOneSide, charCount);
printRightBorder(width, charCount, spacesOneSide);
charCount+=2;
spacesOneSide--;
while(heightOutput> 0)
int freeSpacesEachSide = ((width-3) / 2);
printLeftBorder(heightOutput);
printLine(freeSpacesEachSide, 3);
printRightBorder(width, 3, freeSpacesEachSide);
heightOutput--;
read.close();
private static void printRightBorder(int width, int charCount, int spacesOneSide)
System.out.print("
private static void printLeftBorder(int number)
");
private static void printLine(int spacesOneSide, int charCount)
printString(" ", spacesOneSide);
printString("*", charCount);
printString(" ", spacesOneSide);
private static void printString(String string, int times)
for(int i = 0; i < times; i++)
System.out.print(string);
Oh and close your scanners. If you have any questions regard my answer, feel free to ask, slow day :)
Happy Coding
First of all thanks for the effort. Your code is understandable! I think I got too hung up on small problems and forgot to focus on proper code structure doing so. I feel like I would have had a much easier time finding logical errors this way. And of course, I should close my Scanner!!! Thanks for the help
– sonti yo
Nov 13 '18 at 14:11
That's usually the way it goes. When you are stuck chasing small errors, take a step back and restructure your code. Either it's a stupid -1 you are missing or your structure is needlessly complicated.
– user10419911
Nov 13 '18 at 14:47
add a comment |
There is a lot going on here. I took the liberty of refactoring your program a bit/lot. Each time you use the same expression multiple times like (width-3)/2 think about what this value means to you and pass it into a variable with a name.
Same with Lines you use multiple times, like your left and right borders and so on.
If you do this your program should become less numbers and more semantic constructs with meaning you can understand. This is where I arrived at and voila it works. :D
import java.util.Scanner;
public class SchleifenTestat
public static void main(String args)
Scanner read = new Scanner(System.in);
System.out.print("Enter height:");
int height= read.nextInt();
int heightOutput= height;
int root = (int)(height*0.25F);
int width= ((height- root) * 2) -1; //Amount of chars in the widest tree line
int spaces = (width- 1); //Amount of spaces line 1
int spacesOneSide = spaces / 2;
int charCount = 1;
int tree = (height- root);
for(int i = 0; i < tree; i++)
printLeftBorder(heightOutput);
heightOutput--;
printLine(spacesOneSide, charCount);
printRightBorder(width, charCount, spacesOneSide);
charCount+=2;
spacesOneSide--;
while(heightOutput> 0)
int freeSpacesEachSide = ((width-3) / 2);
printLeftBorder(heightOutput);
printLine(freeSpacesEachSide, 3);
printRightBorder(width, 3, freeSpacesEachSide);
heightOutput--;
read.close();
private static void printRightBorder(int width, int charCount, int spacesOneSide)
System.out.print("
private static void printLeftBorder(int number)
");
private static void printLine(int spacesOneSide, int charCount)
printString(" ", spacesOneSide);
printString("*", charCount);
printString(" ", spacesOneSide);
private static void printString(String string, int times)
for(int i = 0; i < times; i++)
System.out.print(string);
Oh and close your scanners. If you have any questions regard my answer, feel free to ask, slow day :)
Happy Coding
There is a lot going on here. I took the liberty of refactoring your program a bit/lot. Each time you use the same expression multiple times like (width-3)/2 think about what this value means to you and pass it into a variable with a name.
Same with Lines you use multiple times, like your left and right borders and so on.
If you do this your program should become less numbers and more semantic constructs with meaning you can understand. This is where I arrived at and voila it works. :D
import java.util.Scanner;
public class SchleifenTestat
public static void main(String args)
Scanner read = new Scanner(System.in);
System.out.print("Enter height:");
int height= read.nextInt();
int heightOutput= height;
int root = (int)(height*0.25F);
int width= ((height- root) * 2) -1; //Amount of chars in the widest tree line
int spaces = (width- 1); //Amount of spaces line 1
int spacesOneSide = spaces / 2;
int charCount = 1;
int tree = (height- root);
for(int i = 0; i < tree; i++)
printLeftBorder(heightOutput);
heightOutput--;
printLine(spacesOneSide, charCount);
printRightBorder(width, charCount, spacesOneSide);
charCount+=2;
spacesOneSide--;
while(heightOutput> 0)
int freeSpacesEachSide = ((width-3) / 2);
printLeftBorder(heightOutput);
printLine(freeSpacesEachSide, 3);
printRightBorder(width, 3, freeSpacesEachSide);
heightOutput--;
read.close();
private static void printRightBorder(int width, int charCount, int spacesOneSide)
System.out.print("
private static void printLeftBorder(int number)
");
private static void printLine(int spacesOneSide, int charCount)
printString(" ", spacesOneSide);
printString("*", charCount);
printString(" ", spacesOneSide);
private static void printString(String string, int times)
for(int i = 0; i < times; i++)
System.out.print(string);
Oh and close your scanners. If you have any questions regard my answer, feel free to ask, slow day :)
Happy Coding
answered Nov 13 '18 at 14:01
user10419911
First of all thanks for the effort. Your code is understandable! I think I got too hung up on small problems and forgot to focus on proper code structure doing so. I feel like I would have had a much easier time finding logical errors this way. And of course, I should close my Scanner!!! Thanks for the help
– sonti yo
Nov 13 '18 at 14:11
That's usually the way it goes. When you are stuck chasing small errors, take a step back and restructure your code. Either it's a stupid -1 you are missing or your structure is needlessly complicated.
– user10419911
Nov 13 '18 at 14:47
add a comment |
First of all thanks for the effort. Your code is understandable! I think I got too hung up on small problems and forgot to focus on proper code structure doing so. I feel like I would have had a much easier time finding logical errors this way. And of course, I should close my Scanner!!! Thanks for the help
– sonti yo
Nov 13 '18 at 14:11
That's usually the way it goes. When you are stuck chasing small errors, take a step back and restructure your code. Either it's a stupid -1 you are missing or your structure is needlessly complicated.
– user10419911
Nov 13 '18 at 14:47
First of all thanks for the effort. Your code is understandable! I think I got too hung up on small problems and forgot to focus on proper code structure doing so. I feel like I would have had a much easier time finding logical errors this way. And of course, I should close my Scanner!!! Thanks for the help
– sonti yo
Nov 13 '18 at 14:11
First of all thanks for the effort. Your code is understandable! I think I got too hung up on small problems and forgot to focus on proper code structure doing so. I feel like I would have had a much easier time finding logical errors this way. And of course, I should close my Scanner!!! Thanks for the help
– sonti yo
Nov 13 '18 at 14:11
That's usually the way it goes. When you are stuck chasing small errors, take a step back and restructure your code. Either it's a stupid -1 you are missing or your structure is needlessly complicated.
– user10419911
Nov 13 '18 at 14:47
That's usually the way it goes. When you are stuck chasing small errors, take a step back and restructure your code. Either it's a stupid -1 you are missing or your structure is needlessly complicated.
– user10419911
Nov 13 '18 at 14:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53281534%2floop-exercise-printing-a-tree-on-console-using-for-loops-stuck-on-details%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What are hoehe and stamm?
– Ralf Renz
Nov 13 '18 at 13:02
Have you tried a debugger? I recommend: perform your algorithm by hand with pen and paper for the smallest example that fails.
– MrSmith42
Nov 13 '18 at 13:03
@RalfRenz "Hoehe" (German word "Höhe", alternatively spelled due to no 'ö' in Java) is the height of something, the one of the tree most likely. "Stamm" means the trunk of the tree.
– deHaar
Nov 13 '18 at 13:04
@qdeHaar: It's totally legal to use 'ö' but I would not recommend it.
– MrSmith42
Nov 13 '18 at 13:06
Im sorry guys. I thought I exchanged all my variables for english names! I forgot some. @deHaar cleared it up, thanks for that. MrSmith42 I thought that aswell. I recently tried out a new editor (Atom) and it wont let me use 'ö'. As you said it shouldnt be used regardless. I should use english names from the getgo anyways, just a bad habit of mine. Also I will try going through it with a pen
– sonti yo
Nov 13 '18 at 13:07