java控制台printf,Java Console printf()方法

Java Console printf()方法

java.io.Console.printf(String fmt, Object... args) 用于将字符串写入控制台输出流。

1 语法

public Console printf(String format, Object... args)

2 参数

format:格式字符串语法中描述的格式字符串

args:格式字符串中格式说明符引用的参数。

3 返回值

返回Console对象。

4 示例

package com.yiidian;

/**

* 一点教程网: http://www.yiidian.com

*/

/**

* java.io.Console.printf(String fmt, Object... args)法的例子

*/

import java.io.Console;

public class Demo {

public static void main(String[] args) {

Console cnsl = null;

try {

cnsl = System.console();

if (cnsl != null) {

String fmt = "%1$4s %2$10s %3$10s%n";

// format

cnsl.printf(fmt, "Items", "Quanity", "Price");

cnsl.printf(fmt, "-----", "-----", "-----");

cnsl.printf(fmt, "Tomato", "1Kg", "15");

cnsl.printf(fmt, "Potato", "5Kg", "50");

cnsl.printf(fmt, "Onion", "2Kg", "30");

cnsl.printf(fmt, "Apple", "4Kg", "80");

}

} catch(Exception ex) {

// if any error occurs

ex.printStackTrace();

}

}

}

输出结果为:

Items Quantity Price

----- -------- -----

Tomato 1Kg 15

Potato 5Kg 50

Onion 2Kg 30

Apple 4Kg 80

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值