如何返回答案?

时间:2019-02-20 15:06:39

标签: java

public class BankAccount {
  private static final double annualInterestRate = 1.5;
  private static double accountBalance = 150;
  private static double MonthlyInterest;


  public static void main(String[] args) {
    // TODO Auto-generated method stub
    calculateMonthlyInterest();   
    System.out.println(MonthlyInterest);
  }

  public static double calculateMonthlyInterest() 
  {
    double MonthlyInterest = (accountBalance * annualInterestRate);
    return MonthlyInterest;

  }
}

这是我的代码,我试图返回MonthlyInterest,但是当我打印出来时,得到的是0而不是225

7 个答案:

答案 0 :(得分:2)

问题

您正在以某种非常奇怪的方式混合使用静态和非静态功能。这是您的代码中发生的事情:

private static double MonthlyInterest;

您在类上创建一个静态变量。由于它是原始double,因此默认存储0。另外,变量名应以小写字母开头。这应该是monthlyInterest。它不会破坏任何内容,但这是区分类名和变量名的标准Java约定。

calculateMonthlyInterest();

您调用calculateMonthlyInterest()方法。

double MonthlyInterest = (accountBalance * annualInterestRate);
return MonthlyInterest;

您计算出每月利息,将其存储在新变量中,然后将其返回。请注意,您的静态MonthlyInterest变量未被此代码更新。相反,您创建的局部变量会“遮盖”静态变量,并优先于该静态变量。

calculateMonthlyInterest();
System.out.println(MonthlyInterest)

您丢弃从未使用过的返回值,然后打印出从初始值零开始从未改变过的静态MonthlyInterest

有两种方法可以使代码正常工作:


具有静态变量

public class BankAccount {
    private static final double annualInterestRate = 1.5;
    private static double accountBalance = 150;
    private static double monthlyInterest;

    public static void main(String[] args) {
        calculateMonthlyInterest();
        System.out.println(monthlyInterest);
    }

    public static void calculateMonthlyInterest() {
        monthlyInterest = (accountBalance * annualInterestRate);
    }
}

带有局部变量

public class BankAccount {
    private static final double annualInterestRate = 1.5;
    private static double accountBalance = 150;

    public static void main(String[] args) {
        double monthlyInterest = calculateMonthlyInterest();
        System.out.println(monthlyInterest);
    }

    public static double calculateMonthlyInterest() {
        return accountBalance * annualInterestRate;
    }
}

答案 1 :(得分:0)

public class BankAccount {
private static final double annualInterestRate = 1.5;
private static double accountBalance = 150;
private static double MonthlyInterest;


public static void main(String[] args) {
    // TODO Auto-generated method stub
    calculateMonthlyInterest();


    System.out.println(MonthlyInterest);
}

public static void calculateMonthlyInterest() 
{
    MonthlyInterest = accountBalance * annualInterestRate ;


}


}

答案 2 :(得分:0)

您无需返回任何值,因为您将其保留在静态变量中。如果希望您的方法实际返回一个值,则将其直接分配给MonthlyInterest。您也不需要在方法内部声明MonthlyInterest,因为这样会将值分配给局部变量,并且打印行会从类静态变量中获取值。

private static final double annualInterestRate = 1.5;
private static double accountBalance = 150;
private static double MonthlyInterest;


public static void main(String[] args) {
    // TODO Auto-generated method stub
    MonthlyInterest = calculateMonthlyInterest();

    System.out.println(MonthlyInterest);
}

public static double calculateMonthlyInterest() 
{
    return  (accountBalance * annualInterestRate);
}

答案 3 :(得分:0)

public static void main(String[] args) {
  double d = calculateMonthlyInterest();
  System.out.println(d);
}

这行吗?

答案 4 :(得分:0)

public static double calculateMonthlyInterest()
{
    MonthlyInterest = (accountBalance * annualInterestRate);
    return MonthlyInterest;
}

尝试

答案 5 :(得分:0)

您正在calculateMonthlyInterest方法内创建一个名为“ MonthyInterest”的新双精度并将其计算值设置为该值,而不是静态变量。您可以通过以下两种方法之一解决此问题:要么不创建新的MonthlyInterest变量,要么像现在一样返回值,然后在主方法中使用返回的值(并删除静态的MonthlyInterest变量)。这两个代码都看起来像这样

public class BankAccount {
private static final double annualInterestRate = 1.5;
private static double accountBalance = 150;
private static double MonthlyInterest;


public static void main(String[] args) {
    // TODO Auto-generated method stub
    calculateMonthlyInterest();


    System.out.println(MonthlyInterest);
}

public static void calculateMonthlyInterest() 
{
    this.MonthlyInterest = (this.accountBalance * this.annualInterestRate);

}


}

public class BankAccount {
private static final double annualInterestRate = 1.5;
private static double accountBalance = 150;


public static void main(String[] args) {
    // TODO Auto-generated method stub
    double MonthlyInterest = calculateMonthlyInterest();


    System.out.println(MonthlyInterest);
}

public static double calculateMonthlyInterest() 
{
    return this.accountBalance * this.annualInterestRate;

}


}

答案 6 :(得分:0)

您两次定义了MonthlyInterest:

  1. 在BankAccount类上
  2. 关于calculateMonthlyInterest方法

这是两个具有相同名称的不同变量,因为varable scope是您要分配的变量(accountBalance * annualInterestRate)是calculateMonthlyInterest方法的局部变量,因此这不会影响在te上定义的变量BankAccount类

您可以解决此问题,在类范围内将返回的calculateMonthlyInterest值分配给MonthlyInterest:

private static final double annualInterestRate = 1.5;
private static double accountBalance = 150;
private static double MonthlyInterest;


public static void main(String[] args) {
  MonthlyInterest = calculateMonthlyInterest();
  System.out.println(MonthlyInterest);
}

public static double calculateMonthlyInterest() {
  return  (accountBalance * annualInterestRate);
}

或未在方法上定义MonthlyInterest:

private static final double annualInterestRate = 1.5;
private static double accountBalance = 150;
private static double MonthlyInterest;


public static void main(String[] args) {
  calculateMonthlyInterest();
  System.out.println(MonthlyInterest);
}

public static double calculateMonthlyInterest() {
  MonthlyInterest = (accountBalance * annualInterestRate);
}