2017-02-12 7 views
-1

do whileループを使用して、中心に*と "o"を含むダイヤモンドを印刷しようとしています。私はすでにForループでこれを行っていますが、whileループに変更する方法は不明です。誰かが何かサポートを与えることができますか?Javaでdo whileループを使用して、中央に*と "o"を含むダイヤモンドを印刷する

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--; 
     } 
    } 

これを行うにはどうすればいいですか?一般に

+0

あなたのクラスメートと一緒に動作するはずです... [彼の質問](http://stackoverflow.com/questions/42179756/printing-diamonds-usingを見てください-stars-and-o-in-java#comment71522651_42179756)。 – QBrute

答えて

0

ここに行く:

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は_pretty-much_と似ています。 – Jarvis

+0

すべてのforループを置き換えて、ループが入れ子になっている間だけできるようにすることはできますか? – donk2017

+0

完了したら、回答を受け入れたものとしてマークします。 @ donk2017 – Jarvis

2

、このようなループのために:

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

:これを行う-whileループに

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

と等しい:

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

このwhileループに等しいです最初のループが発生していることが確かな場合は、以下のdo-whileを使用できます。

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

「do while loop」に変換したい – donk2017

+0

do-whileを追加しました。それはあなたが望むものですか? – Thanasis

関連する問題