简体   繁体   English

如何返回答案?

[英]How do I return the answer?

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;

  }
}

This is my code, I'm trying to return MonthlyInterest but when I print it out I get 0 instead of 225 这是我的代码,我试图返回MonthlyInterest但是当我打印出来时,我得到的是0而不是225

The Problem 问题

You're mixing static and non-static functionality in some pretty odd ways. 您正在以某种非常奇怪的方式混合使用静态和非静态功能。 Here's what's happening in your code: 这是您的代码中正在发生的事情:

private static double MonthlyInterest;

You create a static variable on your class. 您在类上创建一个静态变量。 Since it's a primitive double , it defaults to storing 0 . 由于它是原始double ,因此默认情况下存储0 Also, variable names should start with a lower-case letter. 另外,变量名应以小写字母开头。 This should be monthlyInterest . 这应该是monthlyInterest It won't break anything, but it's a standard Java convention to distinguish class names from variable names. 它不会破坏任何内容,但这是区分类名和变量名的标准Java约定。

calculateMonthlyInterest();

You call the calculateMonthlyInterest() method. 您调用calculateMonthlyInterest()方法。

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

You calculate the monthly interest, store it in a new variable , and return it. 您计算每月利息,将其存储在新变量中 ,然后将其返回。 Note that your static MonthlyInterest variable is not updated by this code . 请注意, 此代码不会更新您的静态MonthlyInterest变量。 Instead, the local variable that you created "shadows" the static variable, and takes precedent over it. 而是,您创建的局部变量会“遮盖”静态变量,并优先于该变量。

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

Your throw away your return value having never used it, and then print out the static MonthlyInterest that was never changed from its initial value of zero. 您丢弃从未使用过的返回值,然后打印出从其初始值零开始从未更改过的静态MonthlyInterest

Here are two different approaches that you can take to make your code work: 您可以采用两种不同的方法来使代码正常工作:


With static variables 带有静态变量

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);
    }
}

With local variables 带有局部变量

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;
    }
}
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 ;


}


}

You don't need to return any value since you are keeping it in a static variable. 您无需返回任何值,因为您将其保留在静态变量中。 If you want your method to actually return a value, assign it directly to MonthlyInterest . 如果您希望您的方法实际返回一个值,则将其直接分配给MonthlyInterest You also don't need to declare MonthlyInterest inside your method, since then the value is assigned to the local variable, and the printline takes value from the class static variable. 您也不需要在方法中声明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);
}
public static void main(String[] args) {
  double d = calculateMonthlyInterest();
  System.out.println(d);
}

Does this work? 这样行吗?

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

try this 尝试这个

You are creating a new double called 'MonthyInterest' inside your calculateMonthlyInterest method and setting your calculated value to that, instead of your static variable. 您将在calculateMonthlyInterest方法内创建一个名为“ MonthyInterest”的新双精度并将其计算值设置为该值,而不是静态变量。 You could fix this one of two ways: either don't create a new MonthlyInterest variable or return the value as you are now, and use that returned value in your main method (and delete the static MonthlyInterest variable). 您可以通过以下两种方法之一解决此问题:不创建新的MonthlyInterest变量或不像现在那样返回值,然后在主方法中使用该返回值(并删除静态的MonthlyInterest变量)。 The code for both of those would look like this 这两个代码都看起来像这样

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);

}


}

or 要么

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;

}


}

You are defining MonthlyInterest 2 times: 您正在定义MonthlyInterest 2次:

  1. On the BankAccount class 在BankAccount类上
  2. On the calculateMonthlyInterest method 在calculateMonthlyInterest方法上

these are two different variables with the same name becouse of the varable scope the one that you are assigning (accountBalance * annualInterestRate) is the one local to the calculateMonthlyInterest method so this doesn't affect the one defined on te BankAccount class 这是两个具有相同名称的不同变量,因为变量范围是您要分配的变量 (accountBalance * annualInterestRate)是calculateMonthlyInterest方法的局部变量,因此这不会影响在BankAccount类上定义的那个变量

You can fix this assigning the returned value of calculateMonthlyInterest to the MonthlyInterest on the class scope : 您可以解决此问题,在类范围内将返回的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);
}

or not defining MonthlyInterest on the method: 或未在该方法上定义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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM