Last active
October 15, 2019 11:51
-
-
Save TorstenDittmann/f67189693dae760a093791d71be9a8c4 to your computer and use it in GitHub Desktop.
JAVA - Weihnachtsbaum in Konsole
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package christmastree; | |
import java.util.Scanner; | |
/** | |
* | |
* @author torstendittmann | |
*/ | |
public class ChristmasTree { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
String green = "\033[32;1m"; | |
String yellow = "\033[0;33m"; | |
String purple = "\033[0;35m"; | |
String reset = "\033[0m"; | |
System.out.println("Bitte gib die Anzahl der Reihen ein:"); | |
int y = sc.nextInt(); | |
int x = ((y - 1) * 2) + 1; | |
int object = 1; | |
System.out.println("y: " + y); | |
System.out.println("x: " + x); | |
System.out.println(); | |
// Baumkrone | |
for (int i = 1; i < y + 1; i++) { | |
System.out.print(new String(new char[(x - object) / 2]).replace('\0', ' ')); | |
String row = new String(new char[object]).replace('\0', '*'); | |
// row zu char array | |
char[] characters = row.toCharArray(); | |
int rand = (int) (Math.random() * row.length()); | |
// ersetze zufälliges Zeichen mit Licht | |
characters[rand] = (char) 0x2BCC; | |
// char array zu row | |
row = new String(characters); | |
row = row.replace( | |
String.valueOf(characters[rand]), | |
reset | |
+ yellow | |
+ String.valueOf(characters[rand]) | |
+ reset | |
+ green); | |
System.out.print(green + row + reset); | |
System.out.println(); | |
object += 2; | |
} | |
// Stamm | |
object = ((y - 1) / 2) + 1; | |
for (int i = 0; i < (y * 0.1); i++) { | |
System.out.print(new String(new char[(x - object) / 2]).replace('\0', ' ')); | |
System.out.print( | |
purple | |
+ new String(new char[object]).replace('\0', '#') | |
+ reset); | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment