0

これは、A、B、C、D という名前で三角形を上下に出力する Java コードです。私の質問は、それらを*同じレベル*に印刷する方法です*

public class ex_5_10 {
public static void main(String args[]){
    // (A)
    System.out.println("(A)") ;

    for(int row = 0 ; row < 10 ; row++){
        for(int column = 0 ; column < 10 ; column++){
            if(column > row) continue ;
            System.out.print("*");
        }
        System.out.println() ;
    }
    //*********************************
    // (B)
    System.out.println("(B)") ;

    for(int row = 0 ; row < 10 ; row++){
        for(int column = 0 ; column < 10 ; column++){
            if(column < row) continue ;
            System.out.print("*");
        }
        System.out.println() ;
    }
    //********************************
    //(C)
    System.out.println("(C)") ;
    for(int row = 0 ; row < 10 ; row++){
        for(int column = 0 ; column < 10 ; column++){
            if( column < row ) System.out.print(" ") ;
            else System.out.print("*");
        }
        System.out.println() ;
    }
    // (D)
    System.out.println("(D)") ;
    for(int row = 0 ; row < 10 ; row++){
        for(int column = 10 ; column >= 0 ; column--){
            if( column > row ){ System.out.print(" ") ; }
            else {System.out.print("*"); }
        }
        System.out.println() ;
    }


}

}

したがって、上記のJavaコードはこれを出力します:

*
**
***

***
**
*

***
 **
  *

  *
 **
***

今、同じ図を同じレベルに印刷する必要があります!

*    *** ***   *   
**   **   **  **
***  *     * ***

私を助けてください!あなたの答えを探しています!前もって感謝します

4

10 に答える 10

5

こんな風に印刷してみませんか?

System.out.println("*    *** ***   *") ;
System.out.println("**   **   **  **") ;
System.out.println("***  *     * ***") ;

更新:わかりました。

これがループの宿題である場合。マトリックス(配列の配列)を作成し、マトリックスの割り当てられた要素で印刷するだけです。

次に、行列を 1 行ずつ出力します。

印刷サイクルごとにオフセットを使用する必要があります。Fox の例、2 番目の疑似コード (Java コードではありません!):

//*********************************
// (B)
matrix[0][offset] = "B";

for(int row = 0 ; row < height ; row++){
    for(int column = 0 ; column < width ; column++){
        if(column < row) continue ;
        matrix[row][column+offset] = "*";
    }
}
offset += width + space_length;
于 2012-05-11T10:34:14.723 に答える
3

次のようなものを試すこともできます。

for(int row = 0 ; row < 10 ; row++){    

    // A
    for(int column = 0 ; column < 10 ; column++){
        if(column > row) continue ;
        System.out.print("*");
    }
    System.out.print("   ");

    // B
    for(int column = 0 ; column < 10 ; column++){
        if(column < row) continue ;
        System.out.print("*");
    }
    System.out.print("   ");


    // C
    for(int column = 0 ; column < 10 ; column++){
        if( column < row ) System.out.print(" ") ;
        else System.out.print("*");
    }
    System.out.print("   ");

    //D
    for(int column = 0 ; column < 10 ; column++){
        if(column > row) continue ;
        System.out.print("*");
    }

    System.out.println() ;
}

内側の 4 つのループを 1 つのループに変更することで、もっと単純化できるかもしれません。でもそれはあなたの宿題です...

于 2012-05-11T11:07:03.500 に答える
2

これに対する単一の答えはありません。

を呼び出すとprintln()、文字列の最後に改行が出力されるため、その行には何も表示されないことに注意してください。

それを念頭に置いて、最初にさまざまな「図」の出力を組み立て、それらを処理したら、意味のある方法で結果を出力するように、ロジックを分割する必要があります (したがって、各文字の最初の行、次に 2 行目など)。各図の処理中に print を呼び出す代わりに、結果を適切なデータ構造に入れます。

ボーナス マークの場合、表示幅全体に十分なデータが提供されたら、ワードラップする必要があります...

于 2012-05-11T10:34:06.407 に答える
1

結果を試してみたい場合は、次のことができます: (これは宿題です)

ループには次の条件があることがわかります。

  1. if(column < row) continue ; System.out.print("*");
  2. if(column < row) continue ; System.out.print("*");
  3. if( column < row ) System.out.print(" ") ; else System.out.print("*");

  4. if( column > row ){ System.out.print(" ") ; } else {System.out.print("*"); }

それらを混ぜて再配置するだけで、結果を確認できます。また、行の 1 つのループの内側にある列のループの内側も移動し、列にオフセットを追加します。
今回は、4 ではなく 1 ブロックのループを使用する必要があります。

楽しむ。

于 2012-05-11T10:41:13.417 に答える
0

すべての異なる図のコードを同じループに入れてから、各図の間に計算されたスペースを入れることができます。数字の間のスペースのパターンを見つけます。

于 2012-05-11T10:35:44.730 に答える
0

列の内部 for ループを行の 1 つのループ内に移動し、列にオフセットを追加します。内部の場合のロジックは、すべて列 - オフセットになります。

于 2012-05-11T10:35:52.547 に答える
0
public class ex_5_10 {
public static void main(String args[]){
    // (A)
    System.out.println("(A)") ;

    for(int row = 0 ; row < 10 ; row++){
        for(int column = 0 ; column < 10 ; column++){
            if(column > row) continue ;
            System.out.print("*");
        }
        System.out.print() ;
    }
   //*********************************
   // (B)
   System.out.println("(B)") ;

   for(int row = 0 ; row < 10 ; row++){
      for(int column = 0 ; column < 10 ; column++){
         if(column < row) continue ;
         System.out.print("*");
      }
      System.out.print() ;
   }
   //********************************
   //(C)
   System.out.println("(C)") ;
   for(int row = 0 ; row < 10 ; row++){
      for(int column = 0 ; column < 10 ; column++){
          if( column < row ) System.out.print(" ") ;
          else System.out.print("*");
      }
      System.out.print() ;
   }
   // (D)
   System.out.println("(D)") ;
   for(int row = 0 ; row < 10 ; row++){
       for(int column = 10 ; column >= 0 ; column--){
         if( column > row ){ System.out.print(" ") ; }
         else {System.out.print("*"); }
       }
       System.out.print() ;
   }


  }
于 2012-06-07T08:41:55.677 に答える