2016-06-10 15 views
1

so soll ich ein Programm erstellen, das eine Box aus Sternen erstellt und bis jetzt wird es nicht korrekt angezeigt, manchmal sind die Spalten zu klein, manchmal zu groß, aber nie wo sie sein müssen. Kann mir bitte jemand helfen!Mein Sternchen-Programm funktioniert nicht richtig

import java.util.Scanner; 

public class DisplayBox { 

    public static void drawBar(int length){ 
     for (int i = 1; i <= length; i++){ 
      System.out.print("*"); 
     } 

    } 
    public static void drawHeight(int height, int length){ 
     int h = 0; 
     while (h++ < length - 2){ 
      System.out.print("*"); 
      int h1 = 0; 
      while (h1++ < length - 2){ 
       System.out.print(" "); 
      } 
      System.out.println(" *"); 

     } 
    } 

    public static void main(String[] args){ 
     Scanner input = new Scanner(System.in); 
     System.out.print("Please enter the length of the Box: "); 
     int length = input.nextInt(); 
     System.out.println("Please enter the height!: "); 
     int height = input.nextInt(); 
     System.out.println(); 
     drawBar(length); 
     drawHeight(height, length); 
     drawBar(length); 
    } 
} 

Antwort

0
  • Sie die Linie
  • Sie h eher length Vergleich zu Ende keine println in drawBar haben als height in drawHeight

nur einen Stil Kommentar: es könnte klarer sein Wenn Ihre Methode eine Zeichenfolge mit einer bestimmten Länge erstellt und die Druckmethoden dann an einem Ort sind:

private String repeat(char ch, int len) { 
    char chars = new char[len]; 
    Arrays.fill(chars, ch); 
    return new String(chars); 
} 

println(repeat('*', length)); 
for (int l = 0; l < length - 2; l++) 
    println("*" + repeat(' ', length - 2) + "*"); 
println(repeat('*', length)); 
0
import java.util.Scanner; 

public class DisplayBox { 

    public static void drawBar(int length){ 
     for (int i = 1; i <= length; i++){ 
      System.out.print("* "); 
     } 
     System.out.print("\n"); 
    } 
    public static void drawHeight(int height, int length){ 
     int h = 0; 
     while (h++ < length - 1){ 
      System.out.print("*"); 
      int h1 = 0; 
      while (h1++ < length-1){ 
       System.out.print(" "); 
      } 
      System.out.println("*"); 

     } 
    } 

    public static void main(String[] args){ 
     Scanner input = new Scanner(System.in); 
     System.out.print("Please enter the length of the Box: "); 
     int length = input.nextInt(); 
     System.out.println("Please enter the height!: "); 
     int height = input.nextInt(); 
     System.out.println(); 
     drawBar(length); 
     drawHeight(height, length); 
     drawBar(length); 
    } 
} 

Dies scheint ok .. obwohl