现在,我正在进行一项练习,要求我使用几种不同的方法,并在main
方法中调用所有这些方法。该代码专门要求用户提交申请状态和年收入,以便用他们自己的单独方法计算用户的扣除金额,税率,应税收入和税额。
然后在main
方法中,我需要调用这些方法并根据用户输入的内容打印出它们返回的值。这就是我到目前为止所做的:
import java.util.*;
public class TaxActivity{
public static double getDeductionAmount(double filingStatus){
Scanner sc = new Scanner(System.in);
int deductionAmount;
if (filingStatus==1){
deductionAmount=6350;
}else if(filingStatus==2){
deductionAmount=12700;
}else{
deductionAmount=9350;
}
return deductionAmount;
}
public static double getTaxBracket(double filingStatus, double yearlyIncome){
Scanner sc = new Scanner(System.in);
double taxRate;
if (filingStatus==1){
if (yearlyIncome>=0&&yearlyIncome<9325){
taxRate=10.0;
}else if(yearlyIncome>=9325&&yearlyIncome<37950){
taxRate=15.0;
}else if(yearlyIncome>=37950&&yearlyIncome<91900){
taxRate=25.0;
}else if(yearlyIncome>=91900&&yearlyIncome<191650){
taxRate=28.0;
}else if(yearlyIncome>=191650&&yearlyIncome<416700){
taxRate=33.0;
}else if(yearlyIncome>=416700&&yearlyIncome<418400){
taxRate=35.0;
}else{
taxRate=39.6;
}
}else if(filingStatus==2){
if (yearlyIncome>=0&&yearlyIncome<18650){
taxRate=10.0;
}else if(yearlyIncome>=18650&&yearlyIncome<75900){
taxRate=15.0;
}else if(yearlyIncome>=75900&&yearlyIncome<153100){
taxRate=25.0;
}else if(yearlyIncome>=153100&&yearlyIncome<233350){
taxRate=28.0;
}else if(yearlyIncome>=233350&&yearlyIncome<416700){
taxRate=33.0;
}else if(yearlyIncome>=416700&&yearlyIncome<470700){
taxRate=35.0;
}else{
taxRate=39.6;
}
}else{
if (yearlyIncome>=0&&yearlyIncome<13350){
taxRate=10.0;
}else if(yearlyIncome>=13350&&yearlyIncome<50800){
taxRate=15.0;
}else if(yearlyIncome>=50800&&yearlyIncome<131200){
taxRate=25.0;
}else if(yearlyIncome>=131200&&yearlyIncome<212500){
taxRate=28.0;
}else if(yearlyIncome>=212500&&yearlyIncome<416700){
taxRate=33.0;
}else if(yearlyIncome>=416700&&yearlyIncome<444550){
taxRate=35.0;
}else{
taxRate=39.6;
}
}
return taxRate;
}
public static double getTaxableIncome(double deductionAmount, double yearlyIncome){
Scanner sc = new Scanner(System.in);
double taxableIncome= yearlyIncome - deductionAmount;
return taxableIncome;
}
public static double getTaxAmount(double taxableIncome, double taxRate){
double taxAmount=(double)taxableIncome*taxRate;
return taxAmount;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter your filing status: ");
System.out.println("1. Single ");
System.out.println("2. Married Filing Jointly ");
System.out.println("3. Head of Household ");
System.out.println("");
System.out.print("Enter either 1, 2, or 3: ");
double filingStatus = sc.nextDouble();
System.out.println("");
System.out.println("Enter your yearly income: ");
double yearlyIncome = sc.nextDouble();
System.out.println("");
System.out.println("Your Deduction Amount is: $"+getDeductionAmount(filingStatus));
System.out.println("Your Tax Rate is: "+getTaxBracket(filingStatus,yearlyIncome)+"%");
System.out.println("Your Taxable Amount is: $"+getTaxableIncome(deductionAmount,yearlyIncome));
System.out.println("Your Amount of Tax Due is: $"+getTaxAmount(taxableIncome,taxRate));
}
}
但是,当我运行代码时,我会收到这些错误:
TaxActivity.java:96: error: cannot find symbol
System.out.println("Your Taxable Amount is: $"+getTaxableIncome(deductionAmount,yearlyIncome));
symbol: variable deductionAmount
location: class TaxActivity
TaxActivity.java:97: error: cannot find symbol
System.out.println("Your Amount of Tax Due is: $"+getTaxAmount(taxableIncome,taxRate));
symbol: variable taxableIncome
location: class TaxActivity
TaxActivity.java:97: error: cannot find symbol
System.out.println("Your Amount of Tax Due is: $"+getTaxAmount(taxableIncome,taxRate));
symbol: variable taxRate
location: class TaxActivity
3 errors
我仍然从Java开始,所以调用方法对我来说仍然是一个新概念,但我似乎无法看到可能导致这些错误的原因。我确保使所有变量都是相同的类型,并为所有方法使用正确的参数。我能想到的唯一可能导致问题的是代码的格式错误,我无法注意到。如果你们中的任何一个人能够指出导致这个错误的原因,我会很感激。
答案 0 :(得分:1)
您无法在另一个方法中创建的一个方法中访问变量。
您的主要方法有:
deductionAmount
但主方法中没有变量int deductionAmount = getDeductionAmount(filingStatus);
。您确实运行了计算它的函数,但您从未将它保存到main方法中的变量中。你应该做的:
taxableIncome
与http://
类似。
答案 1 :(得分:0)
您应该在{main}方法中存储getDeductionAmount(filingStatus)
方法的返回值
就像浮动deductionAmount = getDeductionAmount(filingStatus)
一样,你必须将你的扣减金额传递给getTaxableIncome(deductionAmount,yearlyIncome)
方法。
同样,您应该存储应税收入和taxRate。
然后用打印和传递给其他方法的变量做你想做的事。