I am trying to print a tree inside a rectangular frame, while my methods completely work i cant figure out on how to implement them with each other.
I have tried to print the tree method instead of spaces in the frame method but it doesnt work and now i cant figure out on what to do.
i am currently learning java so its still pretty new territory for me.
import java.util.Scanner;
public class Weihnachtsbaum {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number between 1 and 20: ");
int height = scan.nextInt();
while((height < 1) || (height > 20))
{
System.out.println("Number not inside parameters!");
System.out.print("Enter a number between 1 and 20: ");
height = scan.nextInt();
}
//this is only to test that the outputs are right
tree(height);
System.out.println();
frame(height);
}
static void tree(int height)
{
for(int i = 1;i <=height; i )
{
//printing star
if(i == 1)
{
for (int j = height - 2; j > 0; j--)
{
System.out.print(" ");
}
for (int k = 1; k > 0; k--)
{
System.out.println(">()<");
}
}
//printing spaces
for (int space = 1;space <= height - i; space )
{
System.out.print(" ");
}
//branches
for (int branch = 1; branch <= i; branch )
{
System.out.print("/\\");
}
//next line
System.out.println();
//lower part of tree
if(i == height)
{
for(int j = (height-1);j > 0;j--)
{
System.out.print(" ");
}
for(int k = 2;k > 0;k--)
{
System.out.print("|");
}
}
}
}
static void frame(int height)
{
for(int rows = 0; rows <= height 4; rows )
{
for(int columns = 0; columns <= height*2 4; columns )
{
if((rows == 0 && columns == 0) || (rows == height 4 && columns==0) || (columns == height*2 4 && rows == 0) || (rows == height 4 && columns == height*2 4))
{
System.out.print(" ");
}
else if( rows == 0 || rows == height 4)
{
System.out.print("-");
}
else if(columns == 0 || columns == height*2 4)
{
System.out.print("|");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
CodePudding user response:
*NOT AN ANSWER*. But loved the beautiful tree you created here and could not help but print it in color and share it with the community. Thank you @Yeetium for this beautiful work!
Happy Holidays!
P.S.: Please let me know if this is a violation of StackOverflow policies/guidelines and I will be happy to take this down.
CodePudding user response:
I think i might have done it even though the code is not the best looking but this should do it appreciate the help. If you can help me to minimalize the code i would be thankful but if not this should be enough to get approved because it works.
import java.util.Scanner;
public class weihnachtsbaum2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Bitte eine Zahl zwischen 1 und 20 eingeben: ");
int height = scan.nextInt();
while ((height < 1) || (height > 20))
{
System.out.println("Nicht gültige Zahl! ");
System.out.print("Bite eine gültige Zahl zwischen 1 und 20 eingeben: ");
height = scan.nextInt();
}
topFrame(height);
middlePart(height);
bottomPart(height);
}
static void topFrame(int height)
{
//Top of Frame
for (int rows = 0; rows <= height; rows )
{
for (int columns = 0; columns <= height 2; columns )
{
if((rows == 0 && columns == 0) || (rows == height && columns==height 2))
{
System.out.print(" ");
}
else if( rows == 0 || rows == height)
{
System.out.print("-");
}
}
if(rows == height)
{
System.out.println();
}
}
}
static void middlePart(int height)
{
for (int rows = 0; rows <= height-1; rows )
{ //spalten
//printing star
if(rows == 0)
{
System.out.print("|");
for (int j = height; j > 0; j--)
{
System.out.print(" ");
}
for (int k = 1; k > 0; k--)
{
System.out.print(">()<");
}
for(int x = 0; x <= height-1; x )
{
System.out.print(" ");
}
System.out.print("|\n");
}
if(rows >= 0)
{
System.out.print("|");
}
for (int spaceLeft = 0;spaceLeft <= height - rows; spaceLeft )
{
System.out.print(" ");
}
//branches
for (int branch = 0; branch <= rows; branch )
{
System.out.print("/\\");
}
for (int spaceRight = 0;spaceRight <= height - rows; spaceRight )
{
System.out.print(" ");
}
System.out.print("|");
System.out.println();
if(rows == height-1)
{
System.out.print("|");
for(int j = (height);j >= 0;j--)
{
System.out.print(" ");
}
for(int k = 2;k > 0;k--)
{
System.out.print("|");
}
for (int spaceRight = 0;spaceRight <= height; spaceRight )
{
System.out.print(" ");
}
System.out.print("|");
}
}
System.out.println();
}
static void bottomPart(int height)
{
//bottom of Frame
for (int rows = 0; rows <= height; rows )
{ //spalten
for (int columns = 0; columns <= height 2; columns )
{
if((rows == 0 && columns == 0) || (rows == height && columns==height 2))
{
System.out.print(" ");
}
else if( rows == 0 || rows == height)
{
System.out.print("-");
}
}
if(rows == height)
{
System.out.println();
}
}
}
}