1.1 改进程序书写风格

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));

       }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值