正如上面的标题所述 - 我遇到的问题与类中的私有变量有关。私有变量没有返回正确的值。
我的目标是检索myIncome
从income
Class 命名的私有变量,它是这些方法的父级。但是当我尝试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);
主要方法。