| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 977 人关注过本帖
标题:数字问题,解答一下
只看楼主 加入收藏
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
结帖率:41.67%
收藏
已结贴  问题点数:20 回复次数:10 
数字问题,解答一下
我来说一下我具体的问题吧
 lastInterest += balance*annual/12;
我现在就是这个算下来小数点后面没有数字, 只有12.00,而没有12.12,我应该得12.12的,为什么后面是0呢?
 lastInterest += (double)balance*annual/12; 这样也没用
搜索更多相关主题的帖子: 解答 数字 
2009-10-08 00:23
ygp_sfec
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:87
专家分:115
注 册:2009-9-8
收藏
得分:3 
你把表达式等号右边的整型数乘上1.0试试
2009-10-08 01:32
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
还是不行
2009-10-08 06:23
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:3 
将12改为12.0

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2009-10-08 08:06
freish
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1223
专家分:437
注 册:2007-6-1
收藏
得分:3 
把整个环境都说说吧
比如你的那些变量的值都是多少

[url=http://shop63425653./]/cvbnm/a6/1d/f4/7dd1720119cf3b1dcfb570b467b24051.jpg" border="0" />[/url]
2009-10-08 08:13
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
这个是整个程序
我现在这个程序小数点后面的数字无法显示,比如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()));
   }
}
2009-10-08 08:44
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:3 
支持  版主!!!
lz以下是你程序的结果:
Enter the account's starting balance: 12.12
Enter the savings account's annual interest rate: 12.12
How many months have passed since the account was opened? 12.12
Enter the amount deposited during month 1: 12.12
Enter the amount withdrawn during month 1: 12.12
Enter the amount deposited during month 2: 12.12
Enter the amount withdrawn during month 2: 12.12
Enter the amount deposited during month 3: 12.12
Enter the amount withdrawn during month 3: 12.12
Enter the amount deposited during month 4: 12.12
Enter the amount withdrawn during month 4: 12.12
Enter the amount deposited during month 5: 12.12
Enter the amount withdrawn during month 5: 12.12
Enter the amount deposited during month 6: 12.12
Enter the amount withdrawn during month 6: 12.12
Enter the amount deposited during month 7: 12.12
Enter the amount withdrawn during month 7: 12.12
Enter the amount deposited during month 8: 12.12
Enter the amount withdrawn during month 8: 12.12
Enter the amount deposited during month 9: 12.12
Enter the amount withdrawn during month 9: 12.12
Enter the amount deposited during month 10: 12.12
Enter the amount withdrawn during month 10: 12.12
Enter the amount deposited during month 11: 12.12
Enter the amount withdrawn during month 11: 12.12
Enter the amount deposited during month 12: 12.12
Enter the amount withdrawn during month 12: 12.12
Total deposited: $145.44
Total withdrawn: $145.44
Interest earned: $146.89
Ending balance: $159.01

C#超级群 74862681,欢迎大家的到来!
2009-10-08 11:07
suckdog
Rank: 1
等 级:新手上路
帖 子:130
专家分:0
注 册:2007-9-19
收藏
得分:0 
你如果输入 1000, 0.12, 100, 0, 110, 10, 120, 20结果就不对了,你会发现Interest earned小数点后面是零,其实应该有是36.36好像是
2009-10-08 11:25
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:0 
看了半天,看懂了。
我的神啦。晕了。
结果在这里:
System.out.println("Interest earned: $" + f1.format(acc.getLastInterest()));
大哥,方法错了吧。
acc.getInterestRate()这个方法.

C#超级群 74862681,欢迎大家的到来!
2009-10-08 11:56
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:0 
soryy上面好象说错.
现在你看下:
我那个months输0可以吗?
Enter the account's starting balance: 110
Enter the savings account's annual interest rate: 10
How many months have passed since the account was opened? 0
Total deposited: $0.00
Total withdrawn: $0.00
Interest earned: $91.67
Ending balance: $201.67

C#超级群 74862681,欢迎大家的到来!
2009-10-08 12:19
快速回复:数字问题,解答一下
数据加载中...
 
   



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

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