用java来格式化对象打印一列 ***** CS Deli ***** Unit Price: $4.25 per pound Weight: 2.56 pounds TOTAL: $10.89
时间: 2023-11-10 21:04:25 浏览: 81
可以使用Java中的String.format()方法来格式化对象打印一列。以下是一个示例代码:
```
public class Main {
public static void main(String[] args) {
String itemName = "CS Deli";
double unitPrice = 4.25;
double weight = 2.56;
double total = unitPrice * weight;
String output = String.format("***** %s *****%nUnit Price: $%.2f per pound%nWeight: %.2f pounds%nTOTAL: $%.2f", itemName, unitPrice, weight, total);
System.out.println(output);
}
}
```
输出结果为:
```
***** CS Deli *****
Unit Price: $4.25 per pound
Weight: 2.56 pounds
TOTAL: $10.89
```
阅读全文