输出的方法和问题

问题描述 投票:0回答:1

我正在尝试学习Java技术。 这段代码有什么问题,为什么我不能打印任何东西? 请帮忙)


public class CarLoan {
int carLoan = 10000;
int loanLenght = 3; //This will represent a loan length of 3 years.
int interestRate = 5; //This will represent an interest rate of 5% on the loan
int downPayment = 2000; //This will represent the down payment provided by a user for this car purchase.


 public CarLoan() {
    if (loanLenght <= 0 && interestRate <=0) {
      System.out.println("Error! You must take out a valid car loan");
    } else if (downPayment >= carLoan){
      System.out.println("The car can be paid in full");
    }else{
      int remainingBalance = carLoan - downPayment; 
      int months = loanLenght * 12;
      int monthlyBalance = remainingBalance / months; 
      System.out.println("false");
    }
 }



public static void main(String[] args) {
CarLoan masterTask = new masterTask();
System.out.println(CarLoan);
}
}


我尝试更改方法,使其无效并在其中列出变量

java methods
1个回答
0
投票

masterTask
CarLoan
类型的变量。当您尝试创建
CarLoan
的实例时,您在语句中误用了它。

public static void main(String[] args) {
CarLoan masterTask = new CarLoan();
System.out.println(masterTask);
}

然后你应该创建一个方法

toString()
来打印对象。

你也误解了Java中构造函数的用途。它不用于进行计算。如果你想让一个对象做某事,那么你应该创建方法,然后在创建该对象的实例时调用该方法。变量将对象的实例作为引用,并可用于调用该对象的方法。

© www.soinside.com 2019 - 2024. All rights reserved.