Java 关于如何在主方法内调用多个方法,需要提供建议
现在,我正在做一个练习,要求我使用几种不同的方法,并在Java 关于如何在主方法内调用多个方法,需要提供建议,java,Java,现在,我正在做一个练习,要求我使用几种不同的方法,并在main方法中调用它们。该代码特别要求用户的申报状态和年收入以各自的方式计算用户的扣除额、税率、应纳税所得额和税额。 然后在main方法中,我需要调用这些方法,并根据用户输入的内容打印出它们返回的值。这就是我到目前为止所做的: import java.util.*; public class TaxActivity{ public static double getDeductionAmount(double filingStatus){
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));
}
}
我还在学习Java,所以调用方法对我来说仍然是一个新概念,但我似乎看不出是什么导致了这些错误。我确保所有变量的类型相同,并为所有方法使用正确的参数。我能想到的唯一可能导致问题的是代码格式中的错误,我无法注意到。如果你们中的任何一位能够指出导致此错误的原因,我将不胜感激。您不能在一个方法中访问在另一个方法中创建的变量 您的主要方法有以下几行:
System.out.println(“您的应纳税额为:$”+应纳税所得额(扣除额,年度收入))代码>
但是在您的main方法中没有变量reductionamount
。您确实运行了该函数来计算它,但从未将其保存到main方法中的变量中。你应该做到:
int-declareonamount=getdeclareonamount(归档状态)代码>
类似于应纳税所得额
您应该在main方法中存储getDeclarationAmount(filingStatus)
方法的返回值。
像floatreductionamount=getreductionamount(filingStatus)
一样,您必须将您的扣减金额传递给getaxableincome(reductionamount,yearlyIncome)
方法。
同样,您也应该存储应纳税所得额和税率
然后对这些变量做任何你想做的事情,比如打印和传递给其他方法。你看到了吗?getTaxableIncome(DecregationAmount,yearlyIncome))代码>是一个编译错误,因为您从未在作用域main
内定义变量DecregationAmount
。您的代码不可编译。您需要声明变量扣减额
和可征税收入
,并为其赋值
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