今天的題目沒法更簡單了,在考慮是不是應該有挑選性的選題目做。
題目是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.
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.
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.
100.00489.1212454.121234.10823.05109.205.271542.25839.1883.991295.011.75
$1581.42
import java.math.BigDecimal;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class FinancialManagement {public static void main(String args[]){Scanner in = new Scanner(System.in);List<Float> list = new ArrayList<Float>();int all = 12;while(all > 0){float f = Float.valueOf(in.nextLine());list.add(f);all --;}float sum = 0;for(int i = 0; i < list.size(); i ++){sum += list.get(i);}float average = sum/12;BigDecimal b = new BigDecimal(average);average = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();System.out.println("$"+average);}}
作者:jason0539
微博:
部落格:(轉載請說明出處)