首页 > 解决方案 > 为什么我的程序中只有 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;


    }


}

标签: javaobjectsubclass

解决方案


@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);

    }

推荐阅读