| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 815 人关注过本帖
标题:大家看看小数点后面的数字怎么显示阿
只看楼主 加入收藏
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
结帖率:41.67%
收藏
 问题点数:0 回复次数:3 
大家看看小数点后面的数字怎么显示阿

我现在这个程序小数点后面的数字无法显示,比如12.12,他只显示12.00,谁帮我改一下阿


public class SavingsAccount
{
   private double balance;    // To hold balance
   private double annualInterestRate;// annual interest rate
   private double lastInterest; // To hold total interest rate
   
    //Constructor
   public SavingsAccount(double bal, double rate)
   {
      balance = bal;
      annualInterestRate = rate;
   }
    //Add deposit to the current balance
   public void deposit(double amount)
   {
      balance += amount;
   }
    //Subtract withdraw to the balance
   public void withdraw(double amount)
   {
      balance -= amount;
   }
    //Calculate the total interest
   public void addInterest()
   {
      lastInterest += balance*getInterestRate();
   }
    //Return total balance
   public double getBalance()
   {
      return balance+lastInterest;
   }
    //Return monthly interest rate
   public double getInterestRate()
   {
      return annualInterestRate/12;
   }
    //Return total interest
   public double getLastInterest()
   {
      return lastInterest;
   }
}

——————————————————————————————————————————————————————————

import java.util.Scanner;//Needed for the Scanner class
import java.text.DecimalFormat;//Needed for decimal place

public class SavingsAccountTest
{
   public static void main(String[] args)
   {
      double balance1;   // To hold balance
      double aInterest;   // To hold annual interest
        double month, dep, with; //To hold total months, deposite, withdraw
        double dep1=0, with1=0;//count total deposite and withdraw
      
      Scanner keyboard = new Scanner(System.in);
        DecimalFormat f1 = new DecimalFormat("0.00");
      
      // Get the intial balance
      System.out.print("Enter the account's starting balance: ");
      balance1 = keyboard.nextDouble();
      
      // Get the annual interest rate
      System.out.print("Enter the savings account's annual "
                         +"interest rate: ");
      aInterest = keyboard.nextDouble();
      
      // Get how many months have been passed
      System.out.print("How many months have passed since the "
                         + "account was opened? ");
      month = keyboard.nextDouble();
        
        // Create an instance of the CellPhone class,
      // passing the data that was entered as arguments
      // to the constructor.
      SavingsAccount acc = new SavingsAccount(balance1, aInterest);
        
        for (int n=1; n<=month; n++)
        {
            System.out.print("Enter the amount deposited during month "
                                 + n + ": ");
            dep = keyboard.nextDouble();
            dep1 +=dep;  
            acc.deposit(dep);
            
            System.out.print("Enter the amount withdrawn during month "
                            + n + ": ");
            with = keyboard.nextDouble();
            with1 +=with;
         acc.withdraw(with);
            acc.addInterest();
        }   
        
      // Get the data from the phone and display it.
      System.out.println("Total deposited: $"+ f1.format(dep1));
      System.out.println("Total withdrawn: $" + f1.format(with1));
      System.out.println("Interest earned: $" + f1.format(acc.getLastInterest()));
      System.out.println("Ending balance: $" + f1.format(acc.getBalance()));
   }
}



[ 本帖最后由 suckdog 于 2009-10-7 11:49 编辑 ]
搜索更多相关主题的帖子: 数字 小数点 
2009-10-07 08:42
lixingjiang
Rank: 3Rank: 3
来 自:滁州学院
等 级:论坛游侠
帖 子:104
专家分:184
注 册:2009-4-17
收藏
得分:0 
这个不懂,楼主见谅!

为梦想冲刺
2009-10-09 17:20
qq13579qq
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-12-18
收藏
得分:0 
测试了一下,没有楼主的情况,我想这代码是没问题的吧。
2009-10-14 13:10
dadongzicool
Rank: 6Rank: 6
等 级:贵宾
威 望:11
帖 子:209
专家分:474
注 册:2009-3-20
收藏
得分:0 
虽然没仔细看代吗  但是自己测试一下 结果如下
Enter the account's starting balance: 23.23
Enter the savings account's annual interest rate: 3
How many months have passed since the account was opened? 3
Enter the amount deposited during month 1: 2.3
Enter the amount withdrawn during month 1: 3.2
Enter the amount deposited during month 2: 6.3
Enter the amount withdrawn during month 2: 3.3
Enter the amount deposited during month 3: 3.1
Enter the amount withdrawn during month 3: 3.0
Total deposited: $11.70
Total withdrawn: $9.50
Interest earned: $18.27
Ending balance: $43.70
貌似没有楼主说的问题  楼主是不是喝多了

本人出售软件专业毕业设计(包括论文,代码)题目众多,价格从优联系方式:QQ
1635008514(寻找高校代理)
2009-10-15 03:21
快速回复:大家看看小数点后面的数字怎么显示阿
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012147 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved