I have to write a code that prints a triangle which the length of the last line is the input number. Also the empty spaces have to be filled with dots.
Also, the code has to run until the input is a uneven number.
Sadly, my code doesn't print out the dots or the other stars, but just the last line.
Can someone maybe look over it and give me some hints?
Any help appreciated. Thank you very much in advance.
Here is the code:
import java.util.Scanner;
public class Triangle_new {
//Main-Method with Input-Check
public static void main(String[] args) {
System.out.println("Type in a number to get the triangle:");
Scanner sc = new Scanner(System.in);
int length = sc.nextInt();
if(length < 0 || length%2 == 0) {
System.out.println("Please type in a uneven number or zero to quit. ");
Triangle_Test.check_input(length);
}else{
display_triangle(length);
}
}
//DisplayTriangle-Method
public static void display_triangle(int length) {
int lines = (length+1)/2;
//Height of the Triangle
get_lines(length);
//Dotfiller Left Side
if(lines > 0){
get_dotsleft(lines, length);
}
//Stars-Output
if(lines > 0) {
get_stars(lines);
}
//Dotfiller Right Side
if(lines > 0) {
get_dotsright(lines, length);
}
}
//Constructors for Triangle
public static void get_lines(int length) {
for(int lines = 1; lines <= (length + 1) / 2; lines++){
System.out.println();
}
}
public static void get_dotsleft(int lines, int length){
for(int leftfiller = 1; leftfiller <= ((length + 1) / 2) - lines; leftfiller++) {
System.out.print(".");
}
}
public static void get_stars(int lines) {
for(int stars = 1; stars <= (2 * lines) - 1; stars++) {
System.out.print("*");
}
}
public static void get_dotsright(int lines, int length){
for(int rightfiller = 1; rightfiller <= ((length+1) / 2) - lines; rightfiller++) {
System.out.print(".");
}
}
}
//Triangle_Test Class with Input-Check Method
class Triangle_Test extends Triangle_new {
public static void check_input(int length) {
//Check Input until Input is uneven number
Scanner sc = new Scanner(System.in);
do {
length = sc.nextInt();
if(length%2 == 0 && length != 0) {
System.out.println("Invalid input. Try again.");
}
if(length%2 != 0 && length > 0) {
}
//Triangle Output
if(length%2 != 0){
Triangle_new.display_triangle(length);
}
if(length == 0) {
System.out.println(" -> Zero detected. Program will be shutdown.");
}
}while(length%2 != 0 || length != 0);
}
}