Quellcode
- public class gleichschenkliges_Dreieck {
- /**
- * @author Torben Brodt
- * @version 1.0
- *
- * <p />Malt ein Dreieck aus Sternen (Leerzeichen helfen bei der Ausrichtung)
- * <p />Funktioniert mit Java <= 1.5
- *
- */
- public static void main(String[] args) {
- java.util.Scanner input = new java.util.Scanner(System.in);
- System.out.print("Bitte geben Sie die Hoehe des gleichschenkligen Dreiecks ein: ");
- int eingabe = input.nextInt();
- int anzahl = eingabe;
- //Nullen sind Leerzeichen
- String nullen, sterne="";
- while(anzahl > 0)
- {
- sterne += (anzahl == eingabe) ? "*" : "**";
- anzahl--;
- nullen = "";
- for(int i=0; i<anzahl; i++)
- nullen += " ";
- System.out.println(" "+nullen+sterne);
- }
- }
- }
Alternativer Code:
Quellcode
- public class A4_Dreieck_Sternchen {
- /**
- * @param args
- */
- public static void main(String[] args) {
- int max = 15, sternzahl=1;
- // Ausgabe
- signs(' ', max/2+1);
- System.out.println("*");
- for (int count=max/2; count >= 0; count--)
- {
- sternzahl += 2;
- signs(' ', count);
- signs('*', sternzahl);
- System.out.println();
- }
- }
- static void signs(char sign, int count) {
- for (int i=0; i<count; i++)
- System.out.print(sign);
- }
- }