ZOJ 1048
Financial Management
Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems to have enough. Larry has decided that he needs to grab hold of his financial portfolio and solve his financing problems. The first step is to figure out what's been going on with his money. Larry has his bank account statements and wants to see how much money he has. Help Larry by writing a program to take his closing balance from each of the past twelve months and calculate his average account balance.
Input Format:
The input will be twelve lines. Each line will contain the closing balance of his bank account for a particular month. Each number will be positive and displayed to the penny. No dollar sign will be included.
Output Format:
The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be no other spaces or characters in the output.
Sample Input:
100.00
489.12
12454.12
1234.10
823.05
109.20
5.27
1542.25
839.18
83.99
1295.01
1.75
Sample Output:
$1581.42
package date20180208;
import java.util.Scanner;
public class FinancialManagement {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
float sum = 0;
for (int i=0;i<12;i++){
float a = sc.nextFloat();
sum+=a;
}
float avg = sum/12;
//保留两位小数String.format("%.2f", avg).toString()
System.out.print("$");
System.out.println(String.format("%.2f", avg).toString());
sc.close();
}
}
当日笔记:
Floatdouble:
Float 单精度 4字节
Double 双精度 8字节
单精度浮点数有效数字8位
双精度浮点数有效数字16位
单精度浮点的表示范围:-3.40E+38 ~ +3.40E+38
双精度浮点的表示范围:-1.79E+308 ~ +1.79E+308
Int Integer比较:
1、 Integer 是 int 的包装类,int是 java中的 基本数据类型。
2、 Integer 使用时要通过实例化才能使用,int不用
3、 Integer实际是对象的引用,new Integer 实际是生成一个指针指向此对象。而int是直接存储数据。
4、 Integer 默认值为null int默认值为0
不同Integer 和 Integer比较始终为false,因为是指针直接的比较,指针指向的地址不同。
Integer 和 int 比较,java自动进行拆包处理,如果值相同,则为true。
Int 和 int 比较则是值之间的比较。
非new Integer 和 new Integer比较 始终为false 非new Integer 指向常量池中的对象,new Integer 指向堆中新建的对象,两者的内存地址不同
两个非new Integer 进行比较时,如果值在-128—127,则结果为true 否则为false。
因为 非new Integer ,java-128-127会对其进行缓存,
https://www.cnblogs.com/guodongdidi/p/6953217.html
保留2位小数:
double f = 111231.5585;
public void m1() {
BigDecimal bg = new BigDecimal(f);
double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(f1);
}
/**
* DecimalFormat转换最简便
*/
public void m2() {
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(f));
}
/**
* String.format打印最简便
*/
public void m3() {
System.out.println(String.format("%.2f", f));
}
public void m4() {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
System.out.println(nf.format(f));
}