Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java中的阈值和税率计算欠税_Java_Algorithm_Methods - Fatal编程技术网

使用java中的阈值和税率计算欠税

使用java中的阈值和税率计算欠税,java,algorithm,methods,Java,Algorithm,Methods,作为学校作业的一部分,我必须计算一个数字,这是公民所欠的总税款。我在一个文本文件中得到了阈值的数量、阈值和每个阈值的速率。例如 Threshold Rate $15,000 15% $29,000 20% $50,000 25% 净收入为55000美元的公民将支付: $15000*0%+14000*15%+21000*20%+5000*25%=7550美元 我在这里开始了我的方法 private void computeTaxOwed(Citizen cit, TaxSchedule sk

作为学校作业的一部分,我必须计算一个数字,这是公民所欠的总税款。我在一个文本文件中得到了阈值的数量、阈值和每个阈值的速率。例如

Threshold   Rate
$15,000 15%
$29,000 20%
$50,000 25%
净收入为55000美元的公民将支付: $15000*0%+14000*15%+21000*20%+5000*25%=7550美元

我在这里开始了我的方法

private void computeTaxOwed(Citizen cit, TaxSchedule sked){

    double netIncome = cit.getNetIncome();
    double totTaxPaid = cit.getTotTaxPaid();

    int levels = sked.getNumLevels();
    double[] threshold = sked.getThreshold();
    double[] rate = sked.getRate();

    double taxOwd = 0;

    for(int i = levels; i>0; i--){
        taxOwd = ((netIncome-threshold[i])*rate[i]);
    }
我知道这不会给出正确的输出,我只是不知道如何使这个算法工作。如果我能提取两个数组的值并将它们分别保存到一个单独的变量中,我可以很容易地得到正确的输出,但我认为这是非常混乱的,不是最好的方法

任何想法都将不胜感激

class Main {

    public static void main(String[] args) {

        double netIncome = 55000;

        int levels = 3;
        double[] threshold = {0, 15000, 29000, 50000};
        double[] rate = {0, 15,20,25};

        double taxOwd = 0;

        double taxableIncome = 0;
        double netIncomeLeft = netIncome;

        for (int i = levels; i > 0; i--) {
            taxableIncome = netIncomeLeft - threshold[i];
            taxOwd += taxableIncome * (rate[i]/100);
            netIncomeLeft = threshold[i];
        }
        System.out.println("taxOwd " + taxOwd);
    }
}
或者以更紧凑的方式:

        for (int i = levels; i > 0; i--) {
            taxOwd += ((netIncome - threshold[i]) * (rate[i]/100));
            netIncome = threshold[i];
        }
或者以更紧凑的方式:

        for (int i = levels; i > 0; i--) {
            taxOwd += ((netIncome - threshold[i]) * (rate[i]/100));
            netIncome = threshold[i];
        }

AlfaTek的建议答案不处理0-15000括号内的速率为非零的情况,因为i>0而不是i>=0作为其for循环的终止条件。以下代码段处理此问题:

    double netIncomeLeft = netIncome;
    double taxOwd = 0;

    for (int i = threshold.length - 1; i >= 0; i--) {
        if(netIncomeLeft < threshold[i]) {
            //continue on if current tier is greater than the remainder
            continue; 
        }

        taxOwd += (netIncomeLeft - threshold[i]) * rate[i] / 100.0;
        netIncomeLeft = threshold[i];
    }
double netIncomeLeft=netIncome;
双taxOwd=0;
对于(int i=threshold.length-1;i>=0;i--){
if(netIncomeLeft<阈值[i]){
//如果当前层大于剩余层,则继续
继续;
}
taxOwd+=(netIncomeLeft-阈值[i])*速率[i]/100.0;
netIncomeLeft=阈值[i];
}

AlfaTek的建议答案不处理0-15000括号内的速率非零的情况,因为i>0而不是i>=0作为其for循环的终止条件。以下代码段处理此问题:

    double netIncomeLeft = netIncome;
    double taxOwd = 0;

    for (int i = threshold.length - 1; i >= 0; i--) {
        if(netIncomeLeft < threshold[i]) {
            //continue on if current tier is greater than the remainder
            continue; 
        }

        taxOwd += (netIncomeLeft - threshold[i]) * rate[i] / 100.0;
        netIncomeLeft = threshold[i];
    }
double netIncomeLeft=netIncome;
双taxOwd=0;
对于(int i=threshold.length-1;i>=0;i--){
if(netIncomeLeft<阈值[i]){
//如果当前层大于剩余层,则继续
继续;
}
taxOwd+=(netIncomeLeft-阈值[i])*速率[i]/100.0;
netIncomeLeft=阈值[i];
}

记录公民收入中哪些部分尚未纳税,并在每次通过循环时更新。这是你的线索。:)跟踪公民收入中尚未纳税的部分,并更新每个循环中通过的部分。这是你的线索。:)假设开始的
netIncome
是1000.00,那么
taxOwd
是什么?对我来说好像是退款!假设开始的
netIncome
是1000.00,那么
taxOwd
是什么?对我来说好像是退款!