0

我正在编写这个程序来计算不同税级的总税额,当输入哨兵数据时,我必须将所有税额相加。我的问题是如何添加我的程序输出的所有税款。这是代码:

import java.util.Scanner;
public class TaxIncome {

 public static void main(String[] args) {

     Scanner kbd = new Scanner (System.in);

            final double fedtax1 = 0.10;                                                // Tax rates    
             final double fedtax2 = 0.15;                                                   
             final double fedtax3 = 0.33;       

             final double taxbracket1 = 15000;                                          // Tax Brackets         
             final double taxbracket2 = 60000;

             final double maxtax1 = 1500;                                               // Max tax on brackets
            final double maxtax2 = 8250;



     while(true){

         System.out.println("Please input the income, type a negative number to end");
         double income = kbd.nextDouble();
         double totaltax = 0;
         double sumOftotaltax = 0;

         if(income <= taxbracket1)
              totaltax = income * fedtax1;

         else if (income <= taxbracket2 && income > taxbracket1)
             totaltax = ((income-taxbracket1)* fedtax2)+ maxtax1;  

         else if (income > taxbracket2)
             totaltax = ((income-taxbracket2)*fedtax3)+maxtax2;

                     System.out.println("The total tax is $" +totaltax);

        if  (income < 0) break;

        sumOftotaltax = totaltax; 


        System.out.println("The total of the taxes is $" +sumOftotaltax);

          }



 }
 }
4

1 回答 1

0

创建一个新的总和变量 sumOf_sumOftotaltax,它将所有总和相加。

sumOf_sumOftotaltax += sumOftotaltax;

或者创建一个 ArrayList 来保存每个单独的。

List <String>totalTaxes = new ArrayList<String>();

totalTaxes.add(sumOftotaltax);

但是,如果您希望提供程序执行的总和,则必须将它们保存在文件或数据库中。

于 2015-10-03T18:29:39.487 回答