java - 为什么我的程序中只有 3 个对象中的 1 个运行?
问题描述
我对java很陌生,并试图掌握在彼此之间使用类。所以这是银行的汽车贷款计划。我一直在创建 Bil(Car) 类的三个不同对象,当我通过方法 bilLån() 运行对象时,它只运行最后一个,在这种情况下是“Opel”对象。它确实运行,所以它一定是一个逻辑错误?
班银行Lå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();
}
}
级车
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);
}
推荐阅读
- python - Django视图在shell中给出模型中的对象时给出''int'对象没有属性''
- wordpress - 将引导类添加到 wp_list_pages()?
- dictionary - 添加到地图作为记录 f# 中的属性
- html - 当我更改它们的颜色时,内部链接不起作用
- python - pandas系列中的空字符串在获取字符串中的单词数时计为一个
- admob - 在测试广告上没有使用 Firebase Admob 插件触发印象事件?
- java - 更正 XSD 中嵌套 ComplexType 的 JSON 请求正文
- android - React Native Image 不会在 Android 上的 Animated.View 内旋转
- javascript - 边框效果就像在带有 css3 / html5 的 Windows 10 中一样
- android - 在 Kotlin 中使用共享偏好检索值