Java 为什么我的char taxGroup变量没有在taxGroup方法中更新? 公共课练习五{ 公共静态void main(字符串[]args){ 扫描仪=新的扫描仪(System.in); 双倍最终税=0.0; 字符组=0; int-taxIncome=0; 国际收入=0; 整数扣减=0; 整数金额; 做{ System.out.println(“输入下一个金额:”); 金额=scanner.nextInt(); 如果(金额>0){ 收入+=金额; } 否则,如果(金额=200_000和&taxIncome=100_000){ 分类群='M'; } 否则如果(taxIncome>=50_000){ 分类群='A'; } 否则如果(taxIncome>=20_000){ 分类群='R'; } 否则{ 分类群='P'; } 回归类群; } 我的输出: 收入=228000美元 扣除额=3450美元 应纳税所得额=224550美元 税组=P 正确输出: 收入=228000.0美元 扣除额=3450.0美元 应纳税所得额=224550.0美元 税组=Q
我认为问题出在这个方法中。我在taxGroup方法中将char taxGroup作为参数传递,但它没有被for循环更新。我认为这可能与主方法中声明的变量有关??提前感谢您的帮助Java 为什么我的char taxGroup变量没有在taxGroup方法中更新? 公共课练习五{ 公共静态void main(字符串[]args){ 扫描仪=新的扫描仪(System.in); 双倍最终税=0.0; 字符组=0; int-taxIncome=0; 国际收入=0; 整数扣减=0; 整数金额; 做{ System.out.println(“输入下一个金额:”); 金额=scanner.nextInt(); 如果(金额>0){ 收入+=金额; } 否则,如果(金额=200_000和&taxIncome=100_000){ 分类群='M'; } 否则如果(taxIncome>=50_000){ 分类群='A'; } 否则如果(taxIncome>=20_000){ 分类群='R'; } 否则{ 分类群='P'; } 回归类群; } 我的输出: 收入=228000美元 扣除额=3450美元 应纳税所得额=224550美元 税组=P 正确输出: 收入=228000.0美元 扣除额=3450.0美元 应纳税所得额=224550.0美元 税组=Q,java,Java,我认为问题出在这个方法中。我在taxGroup方法中将char taxGroup作为参数传递,但它没有被for循环更新。我认为这可能与主方法中声明的变量有关??提前感谢您的帮助 正如您从输出中看到的,我的收入、扣减额和应纳税所得额都是正确的。唯一错误的是税组。您需要将taxableIncome()函数的结果存储在变量taxIncome中。以便调用函数taxGroup()具有新计算的taxIncome。当前taxIncome的值仍为0,因为taxIncome未更新。有关实际原因,请参阅链接副本.t
正如您从输出中看到的,我的收入、扣减额和应纳税所得额都是正确的。唯一错误的是税组。您需要将
taxableIncome()
函数的结果存储在变量taxIncome
中。以便调用函数taxGroup()
具有新计算的taxIncome。当前taxIncome
的值仍为0
,因为taxIncome
未更新。有关实际原因,请参阅链接副本.taxIncome
发送到taxGroup
方法的变量不会用新值更新。在taxGroup
方法中打印taxIncome
,您将看到taxIncome
的值。通常,混合本地、全局变量名和方法名是不好的做法。
public class exerciseFive {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
double finalTax = 0.0;
char taxGroup = 0;
int taxIncome = 0;
int income = 0;
int deduction = 0;
int amount;
do {
System.out.println("Enter next amount: ");
amount = scanner.nextInt();
if (amount > 0) {
income += amount;
}
else if (amount < 0) {
deduction -= amount;
}
} while (amount != 0);
System.out.println("Income = $" + income);
System.out.println("Deductions = $" + deduction);
System.out.println("Taxable Income = $" + taxableIncome(income, deduction, taxIncome));
System.out.println("Tax Group = " + taxGroup(taxIncome, taxGroup));
//System.out.println("Tax Owed = $" + computeTax(taxGroup, taxIncome, finalTax));
}
public static int taxableIncome (int income, int deduction, int taxIncome) {
taxIncome = income - deduction;
if (taxIncome <= 0) {
taxIncome = 0;
}
return taxIncome;
}
public static char taxGroup (int taxIncome, char taxGroup) {
if (taxIncome >= 500_000) {
taxGroup = 'S';
}
else if (taxIncome >= 200_000 && taxIncome < 500000) {
taxGroup = 'Q';
}
else if (taxIncome >= 100_000) {
taxGroup = 'M';
}
else if (taxIncome >= 50_000) {
taxGroup = 'A';
}
else if (taxIncome >= 20_000) {
taxGroup = 'R';
}
else {
taxGroup = 'P';
}
return taxGroup;
}
My output:
Income = $228000
Deductions = $3450
Taxable Income = $224550
Tax Group = P
Correct Output:
Income = $228000.0
Deductions = $3450.0
Taxable income = $224550.0
Tax group = Q