System.out.format不起作用 [英] System.out.format not working

查看:74
本文介绍了System.out.format不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使在使用System.out.format后我也无法对齐出错的内容。请告诉我。



even after using System.out.format I am unable to align the things what went wrong pl. let me know.

package arrayformat;

       /**
        *
       * @author Sunil
       */
       public class ArrayFormat {
        static float productPrice[];
        static int productId[];
        static String productName[];
        static String productType[];
        /**
        * @param args the command line arguments
        */
        public static void main(String[] args) {

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


         for (int i=0; i<10; i++){
           System.out.format("%5d", i);
            System.out.println("\t"+productId[i]+"\t"+productName[i]
         +"\t"+productType[i]+"\t"+productPrice[i]);
            }
         }

         }





和out put是



0 1001吉百利巧克力20.0

1 1002 Parker Vector Stationary 150.0

2 1003雀巢咖啡80.0

3 1004 kissan jam Jam 65.0

4 1005草本精油30.0

5 1006 Garnier man's洗脸79.0

6 1007筹码筹码10.0

7 1008饼干饼干20.0

8 1009 Bournvita健康补品140.0

9 1010百事可乐冷饮24.0



请让我知道我在哪里弄错了。



and out put is

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

please let me know where I have made mistake to get it rectify.

推荐答案

参见 https://docs.oracle.com/javase/tutorial/java/data/numberformat.html [<一个href =https://docs.oracle.com/javase/tutorial/java/data/numberformat.html\"target =_ blanktitle =New窗口> ^ ]和 http://docs.oracle。 com / javase / 7 / docs / api / java / util / Formatter.html [ ^ ]。



See https://docs.oracle.com/javase/tutorial/java/data/numberformat.html[^] and http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html[^].

        int[] productId = new int[]{1001,1002,1003 };
        String[] productName= new String[]{"Cadbury","Parker Vector","Nescafe" };
        String[] productType = new String[]{"Chocolate","Stationery","Coffee" };
        float[] productPrice = new float[]{20,150,80 };
        for (int i=0; i < productId.length; i++) {
            System.out.format("%5d", i);
            System.out.format("\t%5d\t%-24s %5.2f\n", productId[i], productName[i] + " " + productType[i], 

productPrice[i]);


这篇关于System.out.format不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆