Dreieck erzeugen - While Schleife

arktis

Neues Mitglied
Hallo Leute,

Ich habe eine Aufgabe bekommen und muss dies bis Montag erledigen. Mit while schleife habe ich es probiert jedoch habe ich bis jetzt nicht geschaft. Ich bitte um eure Hilfe.

Aufgabe ist folgendes Dreieck zu erzeugen:
* * * * * * * * *
* + + + + + + *
* + o o o + *
* + o o + *
* + o + *
* + + *
* + *
* *
*

Mein Code bis jetzt ist:

Java:
public void printDreieck2(int anz)
    {
        int s;
        int z;
        
        z = 0;
        while (z < anz)
        {
            s = 0;
            while (s < anz)
            {
                if ((z == 0) || (s == 0) || (z+s == anz-1 ))
                {
                    System.out.print("*");
                }
                else
                {
                    if((z + s == anz-2))
                    {
                        System.out.print("+");
                    }
                    else
                    if((s+z < anz-1))
                    {
                        System.out.print("o");
                    }
                    else
                    {
                        System.out.print(" ");
                    }
                }                
                s++;
            }
            System.out.println();
            z++;
        }
    }
 

IsSchoGuat

Mitglied
Bist doch schon gut dabei... das schaffst du.... da fehlt fast nichts mehr......
dann wars das auch schon :rtfm:
Viel spass noch
Java:
 } else if(s == 1 &&  anz -z > 1){
                    System.out.print("+");
}...
 
Zuletzt bearbeitet von einem Moderator:

arktis

Neues Mitglied
* * * * * * * * *
* + + + + + + *
* + o o o + *
* + o o + *
* + o + *
* + + *
* + *
* *
*
Nur diese zwei Reihen bringe ich nicht zusammen.
 

IsSchoGuat

Mitglied
Java:
 public static void printDreieck2(int anz){
        int s;
        int z;
        z = 0;
        while (z < anz) {
            s = 0;
            while (s < anz) {
                if ((z == 0) || (s == 0)  || (z+s == anz-1 ) ) {
                    System.out.print("*");
                } else if(s == 1 &&  anz -z > 1){
                    System.out.print("+");
                } else if( z == 1 && s < anz -1){
                    System.out.print("+");

                } else{
                    if((z + s == anz-2)){
                        System.out.print("+");

                    }else{
                        if((s+z < anz-1)){
                            System.out.print("o");
                        }else{
                            System.out.print(" ");
                        }
                    }
                }
                s++;
            }

            System.out.println();
            z++;

        }

    }

so jetzt aber mit plus
 
Zuletzt bearbeitet von einem Moderator:

Neue Themen


Oben