2017-02-13 12 views
-3

whileループからForループへコードを変換しようとしましたが、目的の出力が得られませんでした。whileループからコード全体をJavaのForループに変換する

Forループのコードは次のとおりです。

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

     int noOfRows = DIAMOND_SIZE; 
     int md=noOfRows%2; 


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

     //Printing upper half of the diamond 
     for (int i = noOfRows; i >= 0;i=(i-2)) 
     { 
      //Printing i spaces at the beginning of each row 
      for (int j = 1; j <= i-md; j++) { 
       System.out.print(" "); 
      } 
      //Printing j *'s at the end of each row 
      for (int j = 1; j <= (noOfRows+1-md); j++) { 
       if (i-md==0 && j==midRow+1) { 
        System.out.print("o "); 
       } 
       else { 
        System.out.print("* "); 
       } 
      } 
      System.out.println(); 
     } 

     //Printing lower half of the diamond 
     for (int i = 2; i <= noOfRows;i=(i+2)) { 
      //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=0; j <= (noOfRows); j++) { 
       System.out.print("* "); 
      } 

      System.out.println(); 
     } 
    } 

私が得た出力された:

* * * 
* o * 
    * * * * 

必要出力は次のとおりです。

* 
* o * 
    * 

私が持っていた元whileループ:

public static void diamond2() { 

      System.out.println(""); 
      System.out.println("Output for: While loop"); 

    int noOfRows = DIAMOND_SIZE; 
    int md=noOfRows%2; 

    //Getting midRow of the diamond 
    int midRow = (noOfRows)/2; 
    int i = noOfRows; 
    while(i >= 0){ 
     //Printing i spaces at the beginning of each row 
     int j = 1; 
     while(j <= i-md){ 
      if(i-md==0)break; 
      System.out.print(" "); 
      j++; 
     } 

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

    i = 2; 
    while(i <= noOfRows){ 
    //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 
     while(j <= (noOfRows+1-md)){ 

       System.out.print("* "); 
       j++; 

     } 
       System.out.println(); 
       i=(i+2); 
     } 

    } 

私がここで間違っていることを誰かが理解する手助けができますか?

+1

デバッガがお手伝いします。 –

+0

@AndyTurnerこれはばかげているかもしれませんが、どうすればデバッグできますか? – donk2017

+1

元々持っていたwhileループは何ですか? –

答えて

0

whileループでjを使用したときは、元のwhileループの変数として使用しました。あなたのforループでは、jカウントを投げ捨てている入れ子のforループでjを使用しています。この追加をグローバルint変数に修正し、トップのダイヤモンドループの最後に2を加えます。一番下のループでは、最初のネストされたループの最後に、j +1の値に設定されています。ここにあなたのループのために、完成したコードがどのように見えるかです:私はちょうどしばらくを変更すると、ループのためにループを考える

int count = 0; 
//Printing upper half of the diamond 
for (int i = noOfRows; i >= 0; i-=2) { 
    //Printing i spaces at the beginning of each row 
    for (int j = 1; j <= i-md; j++) { 
     if(i-md==0) { 
      break; 
     } 
     System.out.print(" "); 
    } 
    //Printing j *'s at the end of each row 
    for (int j = noOfRows - count; j <= (noOfRows+1-md); j++) { 
     if (i-md==0 && j==midRow+1) { 
      System.out.print("o "); 
     } 
     else { 
      System.out.print("* "); 
     } 
    } 
    System.out.println(); 
    count += 2; 
} 

//Printing lower half of the diamond 
count = 0; 
for (int i = 2; i <= noOfRows; i+=2) { 
    //Printing i spaces at the beginning of each row 
    for (int j = 1; j <= i; j++) { 
     System.out.print(" "); 
     count = j+1; 
    } 
    //Printing j *'s at the end of each row 
    for (int j = count; j <= (noOfRows); j++) { 
     System.out.print("* "); 
    } 
    System.out.println(); 
} 
0

ループを比較するのではなく、デバッグ時にコードが間違っている場所に集中しておく必要があります。これは問題がどこにあったのかを判断し、元のコードと比較することができました。私に

最後で簡単に見低くダイヤモンドのループでは、次の

//Printing j *'s at the end of each row 
     while(j <= (noOfRows+1-md)){ 

を持って示していますが、ループの最後には、これを持っていながら、

//Printing j *'s at the end of each row 
      for (int j=0; j <= (noOfRows); j++) { 

だから、しばらくあなたはj <= (noOfRows + 1 - md) を比較しているが、ループのためにあなただけのj <= noOfRows

比較している。これは、問題のようです。

+0

私はそれを変更しようとしましたが、違いはありません。 – donk2017

+0

私もデバッグを試みましたが、どこが間違っているのか理解できませんでした。 – donk2017

0

すると意図したものではありません。とにかく、あなたが望むものを達成するための真っ直ぐな方法があります。

JavaでforループAは、3つの部分があります。

  • 初期値
  • 条件各反復
  • アクションの前に我々はwhileからに変換し、これを使用し、各反復

後にfor。最初のステップで

  1. 、すべてのあなたのwhileループを取るだけの条件を満たしてforループによってそれらを交換。例えばfor(;j <= i - md;)
  2. 2番目の手順では、アクションを検索します。ほとんどの場合、あなたのwhileループの最後の行を見つけたら、それをforループの動作部分に入れます。例えばfor(;j <= i - md; j++)である。 注意!これが容易ではない場合があります。
  3. 最後のステップは、初期状態の管理です。変数がループのスコープの外側で使用されないため、forループに初期状態を与えることが可能な場合を特定します。最後の値の割り当てをループの初期状態にすることができます。例えばfor(int j=1;j <= i - md; j++)注意!これが容易ではない場合があります。あなたの場合、このステップは全く意味がありません。

ソリューションは、このようになります。

public static void diamond2() { 

    System.out.println("Solved by Stackoverflow.com. Have fun with it."); 
    System.out.println("Output for: For loops"); 

    int noOfRows = DIAMOND_SIZE; 
    int md = noOfRows % 2; 

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

     // Printing j *'s at the end of each row 
     for (; j <= (noOfRows + 1 - md); j++) { 
      if (i - md == 0 && j == midRow + 1) { 
       System.out.print("o "); 
      } else { 
       System.out.print("* "); 
      } 
     } 
     System.out.println(); 
    } 

    for (i = 2; i <= noOfRows; i = (i + 2)) { 
     // Printing i spaces at the beginning of each row 
     int j = 1; 
     for (; j <= i; j++) { 
      System.out.print(" "); 
     } 
     // Printing j *'s at the end of each row 
     for (; j <= (noOfRows + 1 - md); j++) { 
      System.out.print("* "); 
      j++; 
     } 
     System.out.println(); 
    } 

} 

ところで、あなたのコードが正しくありません。 ダイヤモンドの大きさが等しい場合は機能しません。これを修正したい場合:コードを投げ捨てて、後ろに座っての距離を中間点と考えてください。楽しむ。

関連する問題