【ThinkingInJava】26、java字符串格式化说明符

本文介绍Java中使用Formatter进行字符串格式化的具体方法,并通过Receipt类的实际应用案例,展示如何格式化输出表格样式的收据信息。
/**
* 书本:《Thinking In Java》
* 功能:对于java字符串格式化说明符
* 		format字符串的格式化参数语法如下:
* 		%[argument_index$][flags][width][.precision]conversion
* 文件:Receipt.java
* 时间:2015年4月11日19:40:53
* 作者:cutter_point
*/
package Lesson13_strings;

import java.util.Formatter;

public class Receipt 
{
	private double total = 0;
	private Formatter f = new Formatter(System.out);	//指定输出的目的地
	
	public void printTitle()		//输出标题
	{
		//这个格式第一个%-15s 是说宽度为15的字符串,后面类同,只是-还是不太清楚是个什么
		f.format("%-15s %5s %10s\n", "Item", "Qty", "Price");
		f.format("%-15s %5s %10s\n", "----", "---", "-----");
	}
	
	public void print(String name, int qty, double price)
	{
		f.format("%-15.15s %5d %10.2f\n", name, qty, price);
		total += price;
	}
	
	public void printTotal()
	{
		f.format("%-15.15s %5s %10.2f\n", "Tax", "", total*0.06);
		f.format("%-15s %5s %10s\n", "", "", "------");
		f.format("%-15.15s %5s %10.2f\n", "Total", "", total*1.06);
	}
	
	public static void main(String [] args)
	{
		Receipt receipt = new Receipt();
		receipt.printTitle();
		receipt.print("Jack's Magic Beans", 4, 4.25);
	    receipt.print("Princess Peas", 3, 5.1);
	    receipt.print("Three Bears Porridge", 1, 14.29);
	    receipt.printTotal();
	    Formatter ff = new Formatter(System.out);
	    ff.format("%5d", 998);
	}

}


输出:

Item              Qty      Price
----              ---      -----
Jack's Magic Be     4       4.25
Princess Peas       3       5.10
Three Bears Por     1      14.29
Tax                         1.42
                          ------
Total                      25.06
  998





图书类 分数 10 作者 温彦 单位 山东科技大学 构建一个书类Book,包括名称(字符串),价格(整型),作者(字符串,多个作者当做一个字符串处理),版本号(整型),提供带参数的构造函数Book(String name, int price, String author, int edition),提供该类的toString()和equals()方法,toString方法返回所有成员属性的值的字符串形式,形如“name: xxx, price: xxx, author: xxx, edition: xxx”,当两个Book对象的名称(不关心大小写,无空格)、作者(不关心大小写,无空格)、版本号相同时,认为两者表示同一本书。 Main函数中,读入两本书,输出他们是否相等,打印两本书的信息。 输入描述: 两本书信息 输出描述: 两本书的打印信息 两本书是否相等 裁判测试程序样例: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); Book b1 = new Book(s.next(), s.nextInt(), s.next(), s.nextInt()); Book b2 = new Book(s.next(),s.nextInt(),s.next(),s.nextInt()); System.out.println(b1); System.out.println(b2); System.out.println(b1.equals(b2)); } } /* 你的代码被嵌在这里 */ 输入样例: 在这里给出一组输入。例如: ThinkingInJava 86 BruceEckel 4 CoreJava 95 CayS.Horstmann 10 输出样例: 在这里给出相应的输出。例如: name: ThinkingInJava, price: 86, author: BruceEckel, edition: 4 name: CoreJava, price: 95, author: CayS.Horstmann, edition: 10 false 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB Java (javac) Selection deleted 1 2
最新发布
10-31
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值