Java 我如何返回答案?
这是我的代码,我试图返回Java 我如何返回答案?,java,Java,这是我的代码,我试图返回MonthlyInterest,但当我打印出来时,我得到的是0而不是225您不需要返回任何值,因为您将它保存在一个静态变量中。如果希望方法实际返回值,请直接将其分配给MonthlyInterest。您也不需要在方法中声明MonthlyInterest,因为这样会将值分配给局部变量,并且打印行从类静态变量中获取值 public class BankAccount { private static final double annualInterestRate = 1.5
MonthlyInterest
,但当我打印出来时,我得到的是0而不是225您不需要返回任何值,因为您将它保存在一个静态变量中。如果希望方法实际返回值,请直接将其分配给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 double calculateMonthlyInterest()
{
double MonthlyInterest = (accountBalance * annualInterestRate);
return 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()
{
MonthlyInterest = accountBalance * annualInterestRate ;
}
}
您不需要返回任何值,因为您将它保存在一个静态变量中。如果希望方法实际返回值,请直接将其分配给
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()
{
MonthlyInterest = accountBalance * annualInterestRate ;
}
}
这行吗
public static void main(String[] args) {
double d = calculateMonthlyInterest();
System.out.println(d);
}
这行吗
public static void main(String[] args) {
double d = calculateMonthlyInterest();
System.out.println(d);
}
试试这个
试试这个您正在
CalculateMonthyInterest
方法中创建一个名为“MonthyInterest”的新双精度值,并将您的计算值设置为该值,而不是静态变量。您可以通过以下两种方式解决此问题:要么不创建新的MonthlyInterest
变量,要么像现在一样返回值,并在main方法中使用该返回值(并删除静态MonthlyInterest
变量)。这两个的代码如下所示
public static double calculateMonthlyInterest()
{
MonthlyInterest = (accountBalance * annualInterestRate);
return MonthlyInterest;
}
或
您正在
CalculateMonthyInterest
方法中创建一个名为“MonthyInterest”的新双精度变量,并将计算值设置为该值,而不是静态变量。您可以通过以下两种方式解决此问题:要么不创建新的MonthlyInterest
变量,要么像现在一样返回值,并在main方法中使用该返回值(并删除静态MonthlyInterest
变量)。这两个的代码如下所示
public static double calculateMonthlyInterest()
{
MonthlyInterest = (accountBalance * annualInterestRate);
return MonthlyInterest;
}
或
问题
您以一些非常奇怪的方式混合了静态和非静态功能。下面是代码中发生的情况:
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;
}
}
在类上创建一个静态变量。由于它是一个原语double
,因此它默认存储0
。此外,变量名应以小写字母开头。这应该是monthlyInterest
。它不会破坏任何东西,但它是区分类名和变量名的标准Java约定
private static double MonthlyInterest;
您可以调用calculateMonthlyInterest()
方法
calculateMonthlyInterest();
计算月利息,将其存储在新变量中,然后返回。请注意,您的静态MonthlyInterest
变量不会被此代码更新。相反,您创建的局部变量“隐藏”了静态变量,并取代了它
double MonthlyInterest = (accountBalance * annualInterestRate);
return MonthlyInterest;
您可以丢弃从未使用过的返回值,然后打印出从未从初始值零更改过的静态MonthlyInterest
这里有两种不同的方法可以让代码正常工作:
使用静态变量
局部变量 问题 您以一些非常奇怪的方式混合了静态和非静态功能。下面是代码中发生的情况:
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;
}
}
在类上创建一个静态变量。由于它是一个原语double
,因此它默认存储0
。此外,变量名应以小写字母开头。这应该是monthlyInterest
。它不会破坏任何东西,但它是区分类名和变量名的标准Java约定
private static double MonthlyInterest;
您可以调用calculateMonthlyInterest()
方法
calculateMonthlyInterest();
计算月利息,将其存储在新变量中,然后返回。请注意,您的静态MonthlyInterest
变量不会被此代码更新。相反,您创建的局部变量“隐藏”了静态变量,并取代了它
double MonthlyInterest = (accountBalance * annualInterestRate);
return MonthlyInterest;
您可以丢弃从未使用过的返回值,然后打印出从未从初始值零更改过的静态MonthlyInterest
这里有两种不同的方法可以让代码正常工作:
使用静态变量
局部变量
您正在定义MonthlyInterest 2次:
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;
}
}
或者不在方法上定义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 2次:
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;
}
}
或者不在方法上定义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);
}
您在
calculateMonthlyInterest
中跟踪了MonthlyInterest
。您可能需要删除局部变量的声明(只需将MonthlyInterest=(accountBalance*annualinterestate);
作为方法体)并正确命名变量:MonthlyInterest
,不要将所有都设为静态。返回(accountBalance*annualinterestate);(如果您不需要将其分配给var)您已经声明了两个月的利息。第一个在顶部