-2

正如上面的标题所述 - 我遇到的问题与类中的私有变量有关。私有变量没有返回正确的值。

我的目标是检索myIncomeincomeClass 命名的私有变量,它是这些方法的父级。但是当我尝试objIncome.getIncome();在另一个类中检索 myIncome 时 - 它返回0.00值。

结果应基于 if 语句输出。

import java.util.Scanner;
import java.text.DecimalFormat;

public class income {

    private double income;
    private double myIncome;

    Scanner input = new Scanner(System.in);
    DecimalFormat df = new DecimalFormat("###.##");

    public void summery1(double myIncome, double income, double tax, double nic, double personalTAXAllowance, double personalNICAllowance,double taxed, double niced){
        this.myIncome = myIncome;
        System.out.println("Your income before tax: " + income);
        System.out.println("Personal allowance: " + personalTAXAllowance);
        System.out.println("NIC allowance: " + personalNICAllowance);
        System.out.println("Tax rate: " + tax + " %");
        System.out.println("National insurance rate: " + nic + " %");
        System.out.println("Your annual income after tax: " + df.format(myIncome));
        System.out.println("Your income on monthly basis: " + df.format(myIncome / 12) + "\n");
    }
    public void summery2(double myIncome, double income, double tax, double nic, double personalTAXAllowance, double personalNICAllowance,double taxed, double niced, double additionalNIC, double resault){
        this.myIncome = myIncome;
        System.out.println("Your income before tax: " + income);
        System.out.println("Personal allowance: " + personalTAXAllowance);
        System.out.println("NIC allowance: " + personalNICAllowance);
        System.out.println("Tax rate: " + tax + " %");
        System.out.println("National insurance rate: " + nic + " %");
        System.out.println("Your annual income after tax: " + df.format(myIncome));
        System.out.println("Your income on monthly basis: " + df.format(myIncome / 12) + "\n");
    }


    public void clcSalary(){

        System.out.println("Please enter your annual salary before tax");
        double income = input.nextDouble();

        if (income <= 32010){ 

            double tax = 0.20; 
            double nic = 0.12; 
            double personalTAXAllowance = 9440; 
            double personalNICAllowance = 7748; 

            double taxed = (income - personalTAXAllowance) * tax;
            double niced = (income - personalNICAllowance) * nic;

            myIncome = income - (taxed + niced);

            summery1(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced);

        }
        else if (income > 32010 && income < 150000 ){
            double tax = 0.40; 
            double nic = 0.12; 
            double personalTAXAllowance = 9440; 
            double personalNICAllowance = 7748; 

            double taxed = (income - personalTAXAllowance) * tax;
            double niced = (income - personalNICAllowance) * nic;

                if (income > 41444){
                    double additionalNIC = income - 41444;
                    double resault = additionalNIC * 0.02; 
                    this.myIncome = income - (taxed + niced) + resault;
                    summery2(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced,additionalNIC, resault);

                }
                else{
                    this.myIncome = income - (taxed + niced);
                    summery1(myIncome,income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced);
                }
            }
        else{
            double tax = 0.45; 
            double nic = 0.12; 
            double personalTAXAllowance = 9440; 
            double personalNICAllowance = 7748; 

            double taxed = (income - personalTAXAllowance) * tax;
            double niced = (income - personalNICAllowance) * nic;

                if (income > 41444){
                    double additionalNIC = income - 41444;
                    double resault = additionalNIC * 0.02;
                    this.myIncome = income - (taxed + niced) + resault;
                    summery2(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced,additionalNIC, resault);
                }
                    else{
                        this.myIncome = income - (taxed + niced);
                        summery1(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced);
                    }
        }
    }
    public void clcHourlyRate(double income){

        System.out.println("Please enter your hourly rate: \n");
        double hourlyRate = input.nextDouble();
        System.out.println("Please enter the hours you've worked this week \n");
        double hoursWeek = input.nextDouble();

        income = ((hourlyRate * hoursWeek) * 4) * 12;

        if (income <= 32010){ 

            double tax = 0.20; 
            double nic = 0.12; 
            double personalTAXAllowance = 9440; 
            double personalNICAllowance = 7748; 

            double taxed = (income - personalTAXAllowance) * tax;
            double niced = (income - personalNICAllowance) * nic;
            this.myIncome = income - (taxed + niced) / 12;
            summery1(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced);
        }
        else if (income > 32010 && income <= 150000 ){

            double tax = 0.40;
            double nic = 0.12; 
            double personalTAXAllowance = 9440; 
            double personalNICAllowance = 7748;

            double taxed = (income - personalTAXAllowance) * tax;
            double niced = (income - personalNICAllowance) * nic;


                if (income > 41444){
                    double additionalNIC = income - 41444;
                    double resault = additionalNIC * 0.02;
                    this.myIncome = ((income - (taxed + niced)) / 12) + resault;
                    summery2(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced,additionalNIC, resault);
                }
                    else{
                        myIncome = (income - (taxed + niced)) / 12;
                        myIncome = income - (taxed + niced) / 12;
                        summery1(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced);
                    }
                }
        else{
            double tax = 0.45; 
            double nic = 0.12; 
            double personalTAXAllowance = 9440; 
            double personalNICAllowance = 7748; 

            double taxed = (income - personalTAXAllowance) * tax;
            double niced = (income - personalNICAllowance) * nic;

                if (income > 41444){

                    double additionalNIC = income - 41444;
                    double resault = additionalNIC * 0.02;
                    this.myIncome = ((income - (taxed + niced)) / 12) + resault;

                    summery2(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced,additionalNIC, resault);
                }
                    else{
                        this.myIncome = (income - (taxed + niced)) / 12;

                        summery1(myIncome, income, tax, nic, personalTAXAllowance,personalNICAllowance,taxed,niced);
                }
            }
        }
    public double getIncome(){
        return myIncome;
        }
    }

这是整个类的完整代码。

public class savings {

    private double v_sav;

    private DecimalFormat df = new DecimalFormat("###.##");
    private Scanner input = new Scanner(System.in);
    private income myincome = new income();

    public void setSavings() {

        double income = myincome.getIncome();

        System.out.println(income);

        System.out.println("Please enter the amount of months: ");
        int months = input.nextInt();

        df.format(v_sav = income * months);
        System.out.println("Your savings in " + months + " months"+ "will be: "+ v_sav);

    }

    public double getSavings() {

        return v_sav;
    }
}

这是使用来自 Class Income 对象的 getIncome 方法的类。

public class PFA {
    public static void main(String[] args) {

        int option;

        do{
mainMenu();
            option = input.nextInt();
            if (option > 5){
                System.out.println("Please enter a value between 1 and 5");
            }
            else{
                if (option == 1){
                    menuIncome(v_income);
                }
                else if (option == 2){
                    menuExpenses(n_expenses, c_expenses, v_choice, v_exit);
                }
                else if (option == 3){
                    savings mySavings = new savings();

                    mySavings.setSavings();
                    System.out.println(mySavings.getSavings());
                }
            }
        }while (option != 5);

主要方法。

4

2 回答 2

2

我假设您有一个像这样的驱动程序(这就是您在 3rd 中所拥有的else-if

public static void main(String[] args) {
    savings s = new savings();
    s.setSavings();
    double value = s.getSavings();
}

在这种情况下,当然它会是 0.0 行

double income = myincome.getIncome();

在 中setSavings(),您还没有调用改变 的值的方法myIncome

在你的savings课堂上,

private income myincome = new income();

创建一个新income实例,因为您没有构造函数,所以将实例字段的值初始化myIncome为 0。这就是您返回的值getIncome()

你没有clcSalary()在任何地方打电话。你应该在打电话之前这样做getIncome()

于 2013-05-24T00:13:48.193 回答
0

是否正在打印摘要功能?那里的 myIncome 的价值是多少?

换句话说,如果getIncome在之前被调用,clcSalary那么clcHourlyRate你的变量将保持为 0。

于 2013-05-23T23:49:07.297 回答