16

私は次のコードを書く必要があります:

掛け算の九九説明:12*12までの小学校の掛け算の九九を印刷します

私はコードを書きました:

public class tables {
    public static void main(String[] args) {
        //int[][] table = new int[12][12];
        String table="";
        for(int i=1; i<13; i++){
            for(int j=1; j<13; j++){
                //table[i-1][j-1] = i*j;
                table+=(i*j)+" ";
            }
            System.out.println(table.trim());
            table="";
        }
    }
}

しかし、問題は出力フォーマットにあります。各数値を4の幅にフォーマットした、マトリックスのような出力が必要です(数値は右揃えで、各行の先頭/末尾のスペースを取り除きます)。私はグーグルを試しましたが、私の問題に対する良い解決策は見つかりませんでした。誰かが私を助けることができますか?

4

3 に答える 3

38

format()必要に応じて出力をフォーマットするために使用できます。

    for(int i=1; i<13; i++){
        for(int j=1; j<13; j++){

           System.out.format("%5d", i * j);
        }
        System.out.println();  // To move to the next line.
    }

または、次を使用することもできます:-

System.out.print(String.format("%5d", i * j));

の代わりにSystem.out.format..

これがどのよう%5dに機能するかの説明です -

  • まず、整数を出力しているので、%d整数のフォーマット指定子であるwhichを使用する必要があります。
  • 5in%5dは、出力にかかる合計幅を意味します。したがって、値が5の場合、次のように5つのスペースをカバーするように印刷されます。****5
  • %5d右揃えに使用します。左揃えには、を使用できます%-5d。値の場合5、これは出力を次のように出力します。- 5****
于 2012-10-09T05:57:48.810 に答える
2

私の例では、配列に異なる長さの文字列が含まれているため、文字列を配置できず、異なる配列の他の文字列がコンソールで一致していませんでした。別のコンセプトで、これらの配列をコンソールに配置できます。コードは次のとおりです。

package arrayformat;

 /**
 *
 * @author Sunil
 */
 public class ArrayFormat {



  /**
  * @param args the command line arguments
  */

  public static void main(String[] args) {

  int[] productId = new int[]  
 {1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,};
   String[] productName= new String[]{"Pepsi","kissan jam","Herbal 
 oil","Garnier man's","Lays chips","biscuits","Bournvita","Cadbury","Parker 
 Vector","Nescafe",};
   String[] productType = new String[]{"Cold Drink","Jam","Oil","Face 
 wash","chips","Biscuits","Health 
 Supplement","Chocolate","Stationary","Coffee",};
    float[] productPrice = new float[]{24,65,30,79,10,20,140,20,150,80,}; 


       int productNameMaxlength=0;
   int productTypeMaxlength=0;



    for (String productName1 : productName) {
        if (productNameMaxlength < productName1.length()) {
            productNameMaxlength = productName1.length();
        }
    }


    for (String productType1 : productType) {
        if (productTypeMaxlength < productType1.length()) {
            productTypeMaxlength = productType1.length();
        }
    }



   for(int i=0;i<productType.length;i++)

   { 
              System.out.print(i);

      System.out.print("\t");

              System.out.print(productId[i]);

              System.out.print("\t");

              System.out.print(productName[i]);

       for(int j=0;j<=productNameMaxlength-productName[i].length
     ();j++)

       {

        System.out.print(" ");

       }

       System.out.print("\t");

       System.out.print(productType[i]);

       for(int j=0;j<=productTypeMaxlength-productType[i].length
    ();j++)

       {
           System.out.print(" ");
       }

               System.out.print("\t");

       System.out.println(productPrice[i]);
          }           
      }

    }
   and output is--
  Sr.No  ID     NAME            TYPE               PRICE
   0    1001    Cadbury         Chocolate           20.0
   1    1002    Parker Vector   Stationary          150.0
   2    1003    Nescafe         Coffee              80.0
   3    1004    kissan jam      Jam                 65.0
   4    1005    Herbal oil      Oil                 30.0
   5    1006    Garnier man's   Face wash           79.0
   6    1007    Lays chips      chips               10.0
   7    1008    biscuits        Biscuits            20.0
   8    1009    Bournvita       Health Supplement   140.0
   9    1010    Pepsi           Cold Drink          24.0

質問と回答のブロックが原因で質問をしたところに質問に答えることができないので、私は自分の答えを引用しています。これは私が感じる別の種類の配列形式でした。

于 2015-07-16T18:27:45.527 に答える
0

出力のフォーマットは、System.out.format( ""、 "")メソッドを使用して実行できます。このメソッドには、最初にフォーマットスタイルを定義し、次に印刷する値を定義する2つの入力パラメーターが含まれます。n桁の値を右揃えにしたい場合を考えてみましょう。最初のパラメータ「%4d」を渡します。

左揃えには-ve%-ndを使用します

右揃えには+ve%ndを使用します

 for(int i=1; i<13; i++){
       for(int j=1; j<13; j++){
            System.out.format("%4d", i * j);  //each number formatted to a width of 4 so use %4d in the format method.
    }
    System.out.println();  // To move to the next line.
}
于 2016-08-09T10:07:45.037 に答える