我正在尝试使用 double 方法打印 table ..但无法正常工作
I am trying to print a table with a double method..yet not working
我似乎无法使我尝试打印的 table 停止在预期的 60.000。
打印永远进行 让我相信我已经创建了一个无限循环..
ideia 是打印一个 table,它给出 50000 和 60000 之间的 4 种填充物的值的税,并且每个新行的增量为 50...
public class FinantialAplicationTable {
public static void main(String[] args) {
int status=0;
double taxableIncome=0;
System.out.printf("Taxable Income\tSingle\tMarried Filling Jointly\tMarried Filling Sepratly\tHead Of Household\n");
System.out.printf(" or Qualifing Widower\n");
System.out.print("______________________________________________________________________________________________________\n");
printTable(status,taxableIncome);
}
public static double printTable(int status, double taxableIncome){
double tax1,tax2,tax3,tax4;
for ( taxableIncome=50000;taxableIncome<60000;taxableIncome =taxableIncome+50){
tax1 = 8350*0.10+(33950-8350)*0.15+(taxableIncome-33950);
tax2 = 16700*0.10+(taxableIncome-16700)*0.15;
tax3 = 8350*0.10+(33950-8350)*0.15+(taxableIncome-33950);
tax4 = 11950*0.10+(45500-11950)*015+(taxableIncome-45500);
if (taxableIncome>=50000 && taxableIncome<=60000){
System.out.println(Math.round(taxableIncome)+" "+Math.round(tax1)+" "+Math.round(tax2)+" "+Math.round(tax3)+" "+Math.round(tax4));
}
}
return printTable(status,taxableIncome);
}
}
非常欢迎任何帮助。
提前谢谢你...
修改printTable的方法签名为:
public static void printTable(int status)
删除 return 语句并将 for 循环更改为:
for (double taxableIncome = 50000; taxableIncome < 60000; taxableIncome += 50)
问题出在 printTable 方法中的 return 语句 - 每次到达时,都会再次递归调用相同的方法,此时会创建一个值为 50000 的新本地 taxableIncome 变量,因此打印无限期地继续。
你一直打印的原因是你没有退出检查。你有一个递归函数,它没有给你自己一条出路。要在 60 点退出,您可以这样做:
public static void printTable(int status, double taxableIncome){
double tax1,tax2,tax3,tax4;
for ( taxableIncome=50000;taxableIncome<60000;taxableIncome =taxableIncome+50){
tax1 = 8350*0.10+(33950-8350)*0.15+(taxableIncome-33950);
tax2 = 16700*0.10+(taxableIncome-16700)*0.15;
tax3 = 8350*0.10+(33950-8350)*0.15+(taxableIncome-33950);
tax4 = 11950*0.10+(45500-11950)*015+(taxableIncome-45500);
if (taxableIncome>=50000 && taxableIncome<=60000){
System.out.println(Math.round(taxableIncome)+" "+Math.round(tax1)+" "+Math.round(tax2)+" "+Math.round(tax3)+" "+Math.round(tax4));
}
}
}
您将 return 类型从 double
更改为 void
,因为您不需要 return 任何内容。通过 returning 函数,您将继续 运行 直到 运行 内存不足。
我似乎无法使我尝试打印的 table 停止在预期的 60.000。 打印永远进行 让我相信我已经创建了一个无限循环.. ideia 是打印一个 table,它给出 50000 和 60000 之间的 4 种填充物的值的税,并且每个新行的增量为 50...
public class FinantialAplicationTable {
public static void main(String[] args) {
int status=0;
double taxableIncome=0;
System.out.printf("Taxable Income\tSingle\tMarried Filling Jointly\tMarried Filling Sepratly\tHead Of Household\n");
System.out.printf(" or Qualifing Widower\n");
System.out.print("______________________________________________________________________________________________________\n");
printTable(status,taxableIncome);
}
public static double printTable(int status, double taxableIncome){
double tax1,tax2,tax3,tax4;
for ( taxableIncome=50000;taxableIncome<60000;taxableIncome =taxableIncome+50){
tax1 = 8350*0.10+(33950-8350)*0.15+(taxableIncome-33950);
tax2 = 16700*0.10+(taxableIncome-16700)*0.15;
tax3 = 8350*0.10+(33950-8350)*0.15+(taxableIncome-33950);
tax4 = 11950*0.10+(45500-11950)*015+(taxableIncome-45500);
if (taxableIncome>=50000 && taxableIncome<=60000){
System.out.println(Math.round(taxableIncome)+" "+Math.round(tax1)+" "+Math.round(tax2)+" "+Math.round(tax3)+" "+Math.round(tax4));
}
}
return printTable(status,taxableIncome);
}
}
非常欢迎任何帮助。
提前谢谢你...
修改printTable的方法签名为:
public static void printTable(int status)
删除 return 语句并将 for 循环更改为:
for (double taxableIncome = 50000; taxableIncome < 60000; taxableIncome += 50)
问题出在 printTable 方法中的 return 语句 - 每次到达时,都会再次递归调用相同的方法,此时会创建一个值为 50000 的新本地 taxableIncome 变量,因此打印无限期地继续。
你一直打印的原因是你没有退出检查。你有一个递归函数,它没有给你自己一条出路。要在 60 点退出,您可以这样做:
public static void printTable(int status, double taxableIncome){
double tax1,tax2,tax3,tax4;
for ( taxableIncome=50000;taxableIncome<60000;taxableIncome =taxableIncome+50){
tax1 = 8350*0.10+(33950-8350)*0.15+(taxableIncome-33950);
tax2 = 16700*0.10+(taxableIncome-16700)*0.15;
tax3 = 8350*0.10+(33950-8350)*0.15+(taxableIncome-33950);
tax4 = 11950*0.10+(45500-11950)*015+(taxableIncome-45500);
if (taxableIncome>=50000 && taxableIncome<=60000){
System.out.println(Math.round(taxableIncome)+" "+Math.round(tax1)+" "+Math.round(tax2)+" "+Math.round(tax3)+" "+Math.round(tax4));
}
}
}
您将 return 类型从 double
更改为 void
,因为您不需要 return 任何内容。通过 returning 函数,您将继续 运行 直到 运行 内存不足。