正如上面标题所述 - 我遇到的问题与班级中的私人变量有关。私有变量不会返回正确的值。
我的目标是从myIncome
Class中检索名为income
的私有变量,这是这些方法的父级。
但是当我尝试在另一个类中使用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);
主要方法。
答案 0 :(得分:2)
我假设你有一个喜欢的驱动程序(这是你在第3个else-if
中所拥有的)
public static void main(String[] args) {
savings s = new savings();
s.setSavings();
double value = s.getSavings();
}
在这种情况下,当然它与行
一起为0.0double income = myincome.getIncome();
<{1>}中的,您尚未调用更改setSavings()
值的方法。
在myIncome
课程中
savings
创建一个新的private income myincome = new income();
实例,因为您没有构造函数,所以将实例字段income
的值初始化为0.这是您使用{{{ 1}}。
您没有在任何地方致电myIncome
。你应该在致电getIncome()
之前这样做。
答案 1 :(得分:0)
是否打印了夏日函数? myIncome的价值是什么?
换句话说,如果在getIncome
或clcSalary
之前调用clcHourlyRate
,那么您的变量将保持为0.