我对Java很陌生,它试图掌握彼此之间使用类的技巧。因此,这是汽车的银行贷款计划。我一直在创建类Bil(Car)的三个不同对象,当我通过bilLån()方法运行这些对象时,它仅运行最后一行,在这种情况下为“ Opel” -object。它确实在运行,所以一定是逻辑错误吗?
BankLån类
import java.util.Scanner;
import java.text.DecimalFormat;
public class bankLån {
DecimalFormat df1 = new DecimalFormat("#.##");
double price;
int years;
double interest;
double interestDiv;;
double oneYr = 14.99;
double threeYr = 10.99;
double fiveYr = 6.99;
double tenYr = 2.99;
double monthlyInterCalc;
double monthlyPayment;
double monthlyInterest;
int months = 12;
Scanner input = new Scanner(System.in);
bankLån() {
}
public void carLoan() {
System.out.println();
System.out.println("You've picked: "+Car.carBrand+"!");
price = (Car.carPrice) - (Car.downPay);
System.out.println("With a down payment of "+Car.downPay);
System.out.println("It will cost you: "+price+"!");
System.out.println("Our interests are: ");
System.out.println("1 year = "+oneYr+"% \t 3 years = "+threeYr+"% \t 5 years = "+fiveYr+"% \t 10 years = "+tenYr+"%");
do {
System.out.println("Please input amount of years: ");
years = input.nextInt();
if (years == 1) {
interest = oneYr;
}
else if (years == 3) {
interest = threeYr;
}
else if (years == 5) {
interest = fiveYr;
}
else if (years == 10) {
interest = tenYr;
}
else {
System.out.println("That amount of years ain't available, please try again.");
}
}while(!(years == 1 || years == 3 || years == 5 || years == 10));
interestDiv = interest / 100;
monthlyInterCalc = (price / years) / (months);
monthlyInterest = monthlyInterCalc * interestDiv;
monthlyPayment = monthlyInterCalc + (monthlyInterCalc * interestDiv);
System.out.println("Your interest will be: "+interest+"% and will cost you around: "+df1.format(monthlyInterest)+ " every month.");
System.out.print("The monthly payment will be around: "+df1.format(monthlyPayment)+".");
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Car ferrari = new Car("Ferrari","Red",1000000,50000);
Car volvo = new Car("Volvo","White",170000,40000);
Car opel = new Car("Opel","Blue",40000,10000);
ferrari.carLoan();
opel.carLoan();
volvo.carLoan();
}
}
Class Car
public class Car extends bankLån{
static String carBrand;
static String carColor;
static double carPrice;
static double downPay;
Car(String brand, String color, double price, double downP) {
carBrand = brand;
carColor = color;
carPrice = price;
downPay = downP;
}
}
@@ Farhan Qasim说您需要删除static
。
但是如果要对每个对象进行处理,则需要在方法bilLån()
中传递对象引用,例如
public void bilLån(Bil mBill) {
System.out.println();
System.out.println("You've picked: "+Bil.carBrand+"!");
price = (mBill.carPrice) - (mBill.downPay);
System.out.println("With a down payment of "+mBill.downPay);
System.out.println("It will cost you: "+price+"!");
System.out.println("Our interests are: ");
System.out.println("1 year = "+oneYr+"% \t 3 years = "+threeYr+"% \t 5 years = "+fiveYr+"% \t 10 years = "+tenYr+"%");
do {
System.out.println("Please input amount of years: ");
years = input.nextInt();
if (years == 1) {
interest = oneYr;
}
else if (years == 3) {
interest = threeYr;
}
else if (years == 5) {
interest = fiveYr;
}
else if (years == 10) {
interest = tenYr;
}
else {
System.out.println("That amount of years ain't available, please try again.");
}
}while(!(years == 1 || years == 3 || years == 5 || years == 10));
interestDiv = interest / 100;
monthlyInterCalc = (price / years) / (months);
monthlyInterest = monthlyInterCalc * interestDiv;
monthlyPayment = monthlyInterCalc + (monthlyInterCalc * interestDiv);
System.out.println("Your interest will be: "+interest+"% and will cost you around: "+df1.format(monthlyInterest)+ " every month.");
System.out.print("The monthly payment will be around: "+df1.format(monthlyPayment)+".");
System.out.println();
}
在主要方法中:
public static void main(String[] args) {
// TODO Auto-generated method stub
Bil ferrari = new Bil("Ferrari","Red",1000000,50000);
Bil volvo = new Bil("Volvo","White",170000,40000);
Bil opel = new Bil("Opel","Blue",40000,10000);
ferrari.bilLån(ferrari);
opel.bilLån(volvo);
volvo.bilLån(opel);
}
public class Bil extends bankLån {
static String carBrand;
static String carColor;
static double carPrice;
static double downPay;
在Bil类中,所有变量都是静态的。意味着它们持有的值对于此类的所有不同对象都是相同的。这会导致对象法拉利和沃尔沃具有与opel相同的属性值,因为上一次设置了opel。
在这些变量之前删除static关键字
[carLoan方法正在以静态类而不是其对象的形式访问Car。
System.out.println("You've picked: "+Car.carBrand+"!");
price = (Car.carPrice) - (Car.downPay);
Car不是实例对象,它引用静态类,因此出现错误“无法对非静态字段Bil.carBrand进行静态引用”
要解决此问题,请在您的方法内添加一个Car对象,然后代替类来访问它。
public void carLoan(Car car) {
System.out.println();
System.out.println("You've picked: "+car.carBrand+"!");
price = (car.carPrice) - (car.downPay);
System.out.println("With a down payment of "+car.downPay);
System.out.println("It will cost you: "+price+"!");
System.out.println("Our interests are: ");
System.out.println("1 year = "+oneYr+"% \t 3 years = "+threeYr+"% \t 5 years = "+fiveYr+"% \t 10 years = "+tenYr+"%");
do {
System.out.println("Please input amount of years: ");
years = input.nextInt();
if (years == 1) {
interest = oneYr;
}
else if (years == 3) {
interest = threeYr;
}
else if (years == 5) {
interest = fiveYr;
}
else if (years == 10) {
interest = tenYr;
}
else {
System.out.println("That amount of years ain't available, please try again.");
}
}while(!(years == 1 || years == 3 || years == 5 || years == 10));
interestDiv = interest / 100;
monthlyInterCalc = (price / years) / (months);
monthlyInterest = monthlyInterCalc * interestDiv;
monthlyPayment = monthlyInterCalc + (monthlyInterCalc * interestDiv);
System.out.println("Your interest will be: "+interest+"% and will cost you around: "+df1.format(monthlyInterest)+ " every month.");
System.out.print("The monthly payment will be around: "+df1.format(monthlyPayment)+".");
System.out.println();
}