Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我如何返回答案?_Java - Fatal编程技术网

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次:

  • 关于银行账户类
  • 关于calculateMonthlyInterest方法
  • 这是两个同名的不同变量,因为您正在分配的变量(accountBalance*AnnualInterestate)是calculateMonthlyInterest方法的本地变量,所以这不会影响BankAccount类上定义的变量

    将calculateMonthlyInterest的返回值分配给类作用域上的MonthlyInterest可以解决此问题:

    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次:

  • 关于银行账户类
  • 关于calculateMonthlyInterest方法
  • 这是两个同名的不同变量,因为您正在分配的变量(accountBalance*AnnualInterestate)是calculateMonthlyInterest方法的本地变量,所以这不会影响BankAccount类上定义的变量

    将calculateMonthlyInterest的返回值分配给类作用域上的MonthlyInterest可以解决此问题:

    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)您已经声明了两个月的利息。第一个在顶部