开发者

Required Minimum Distribution Calculation not working

开发者 https://www.devze.com 2023-01-24 16:50 出处:网络
I am trying to make an app for the Android that does RMD calculations.The program, however, keeps giving 0.0.

I am trying to make an app for the Android that does RMD calculations. The program, however, keeps giving 0.0.

I think that my problem lies in this code:

package com.MinDis.android;

import java.util.*;
import java.text.*;

public class RMD
{
 double balance;
 double rmd;
 long age;
 String bdate;
 SimpleDateFormat sd = new SimpleDateFormat("MM/dd/yyyy");

 Date current = new Date();
 public void setBalance(double i)
 {
  balance = i;
 }

 public double getBalance()
 {
  return balance;
 }

 public void setBDate(String h)
 {
  bdate = h;
 }

 public long getAge() throws Exception
 {

  Date birthd = sd.parse(bdate);
  long cur;
  long birth;
  long diff;

  cur = current.getTime();
  birth = birthd.getTime();
  diff = cur - birth;
  age = (diff/(24*60*60*1000))/365;
  return age;
 }

 public do开发者_如何转开发uble getRMD()
 {

  if (age == 70)
  {
   rmd = balance/27.4;
  }

  if (age == 71)
  {
   rmd = balance/26.5;
  }

  if (age == 72)
  {
   rmd = balance/25.6;
  }

  if (age == 73)
  {
   rmd = balance/24.7;
  }

  if (age == 74)
  {
   rmd = balance/23.8;
  }

  return rmd;
 }
}

I tried using .equals(), but it complained that I was trying to dereference the long value. Also, I am aware that java.util.Date is deprecated, but I don't want to try the other classes. From my first Android app, I figured out the "==" does not work, but I am not sure what to do here.

I have verified that the input for a balance and the birthdate values were accepted, so that is how I limited it to this particular class.


I solved the problem. I changed the conditionals to a switch statement, then used

switch((int)getAge())


The problem is that unless something calls getAge, the age field was never getting assigned. Only once getAge is called does it acquire a value. So if getRMD is called before getAge, then none of the if conditions would be true, so rmd would not be set, and thus the original value of 0 would remain. I'd avoid having rmd and age be fields of the class. Instead, they should be local variables of the functions that are calculating them or using them.

0

精彩评论

暂无评论...
验证码 换一张
取 消