2017-02-12 5 views
-1

Ich versuche, einen Diamanten mit * und "o" in der Mitte mit Do While-Schleife zu drucken. Ich habe das schon mit der For-Schleife gemacht, aber ich bin mir nicht sicher, wie ich es in die while-Schleife ändern kann. Kann jemand etwas Unterstützung geben?Drucken eines Diamanten mit * und "o" in der Mitte mit Do While-Schleife in Java

public static void diamond1() { 
     System.out.println("Diamond Height: " + DIAMOND_SIZE); 
     System.out.println("Output for: For Loop"); 

     int noOfRows = DIAMOND_SIZE; 

     //Getting midRow of the diamond 
     int midRow = (noOfRows)/2; 

     //Initializing row with 1 
     int row = 1; 

     //Printing upper half of the diamond 
     for (int i = midRow; i > 0; i--) 
     { 
      //Printing i spaces at the beginning of each row 
      for (int j = 1; j <= i; j++) { 
       System.out.print(" "); 
      } 

      //Printing j *'s at the end of each row 
      for (int j = 1; j <= row; j++) { 
       System.out.print("* "); 
      } 

      System.out.println(); 

      //Incrementing the row 
      row++; 
     } 

     //Printing lower half of the diamond 
     for (int i = 0; i <= midRow; i++) { 
      //Printing i spaces at the beginning of each row 
      for (int j = 1; j <= i; j++) { 
       System.out.print(" "); 
      } 

      //Printing j *'s at the end of each row 
      int mid = (row+1)/2; 
      for (int j = row; j > 0; j--) { 
      if(i==0 && j==mid) { 
       System.out.print("o "); 
      } 
      else { 
       System.out.print("* "); 
      } 
      } 

      System.out.println(); 

      //Decrementing the row 
      row--; 
     } 
    } 

Wie würde ich das machen?

+0

Sie sollten mit Ihrem Mitschülern zusammenarbeiten ... Blick auf [seine Frage] (http://stackoverflow.com/questions/42179756/printing-diamonds-using -Stars-und-o-in-Java # Kommentar71522651_42179756). – QBrute

Antwort

0

Hier gehen Sie:

public static void Diamond() { 
    int DIAMOND_SIZE = 5; 
    System.out.println("Diamond Height: " + DIAMOND_SIZE); 
    System.out.println("Output for: For Loop"); 

    int noOfRows = DIAMOND_SIZE; 

    //Getting midRow of the diamond 
    int midRow = (noOfRows)/2; 

    //Initializing row with 1 
    int row = 1; 

    int i = midRow; 
    while(i > 0){ 
     //Printing i spaces at the beginning of each row 
     int j = 1; 
     while(j <= i){ 
      System.out.print(" "); 
      j++; 
     } 

     //Printing j *'s at the end of each row 
     j = 1; 
     while(j <= row){ 
      System.out.print("* "); 
      j++; 
     } 
     System.out.println(); 

     //Incrementing the row 
     row++; 

     i--; 
    } 

    i = 0; 
    while(i <= midRow){ 
    //Printing i spaces at the beginning of each row 
     int j = 1; 
     while(j <= i){ 
      System.out.print(" "); 
      j++; 
     } 

     //Printing j *'s at the end of each row 
     int mid = (row+1)/2; 
     j = row; 
     while(j > 0){ 
      if(i == 0 && j == mid) 
       System.out.print("o "); 
      else 
       System.out.print("* "); 
      j--; 
     } 

     System.out.println(); 

     //Decrementing the row 
     row--; 
     i++; 
    } 
} 
+0

do ... while ist _pretty-much_ ähnlich wie while. – Jarvis

+0

können Sie alle For-Schleifen ersetzen, so dass nur While-Loops verschachtelt sind? – donk2017

+0

Fertig, markieren Sie jetzt die Antwort als akzeptiert. @ donk2017 – Jarvis

2

Im Allgemeinen ist ein for-Schleife wie folgt aus:

for (command1; statement; command2){ 
    // Code to loop 
} 

diese while-Schleife entspricht:

command1; 
while(statement){ 
    // Code to loop 
    command2; 
} 

und gleich diese do-while-Schleife:

command1; 
do{ 
    if (statement){ 
     // Code to loop 
     command2; 
    } 
} while(true); 

Wenn es sicher ist, dass die erste Schleife passieren muss, können Sie die folgende Do-while verwenden:

command1; 
do{ 
    // Code to loop 
    command2; 
} while(statement); 
+0

Ich möchte es in "Do While-Schleife" konvertieren – donk2017

+0

Ich habe die Do-While hinzugefügt. Ist es das, was du willst? – Thanasis

Verwandte Themen