4 回答
TA贡献1815条经验 获得超10个赞
问题
您正在以一些非常奇怪的方式混合静态和非静态功能。这是您的代码中发生的事情:
private static double MonthlyInterest;
您在类上创建一个静态变量。由于它是一个原始的double,它默认为 storage 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;
}
}
TA贡献1906条经验 获得超3个赞
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 ;
}
}
TA贡献1909条经验 获得超7个赞
您正在定义 MonthlyInterest 2 次:
在 BankAccount 类上
关于 calculateMonthlyInterest 方法
这些是两个具有相同名称的不同变量,因为变量范围是您分配的一个(accountBalance * yearInterestRate)是 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);
}
TA贡献1799条经验 获得超9个赞
您不需要返回任何值,因为您将其保存在静态变量中。如果您希望您的方法实际返回一个值,请将其直接分配给MonthlyInterest. 你也不需要MonthlyInterest在你的方法中声明,因为那时值被分配给局部变量,并且 printline 从类静态变量中获取值。
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);
}
添加回答
举报