Java:如何获取return语句返回所有值

时间:2015-04-30 15:44:13

标签: java

我尝试写一张表格,显示4个不同档案类别下应税收入从50,0000到60,000的应税收入。使用方法,代码中的return语句只打印出四个归档类别列中的一个。请问如何打印出剩余的列?

public class TaxableIncome {

   public static void main(String[] args) {
       System.out.println("Taxable Income\t\t Single\t\t Married Joint\t\t MarriedSeerate\t\t HeadofaHouse");
       System.out.println("-----------------------------------------------------------------------------------------------------");
       double Tincome;
       int profile=1;
       for(Tincome=50000; Tincome<=60000; Tincome+=50) { 
           System.out.println( Tincome +"\t\t" + computetax(profile, Tincome));
       }
    }

    public  static double computetax( int status , double income) {
        double tax;
        double single=0;
        double mjoint=0;
        double mseperate=0;
        double head=0;

        for(status=1;status<=4;status++) {
            if(status==1) {  
                tax = 8350*.10 + (33950-8350)*0.15 + (income- 33950)*0.25;
                single= tax;
            }
            if(status==2) {
                tax = 16700*0.10 + (income-16700)*0.15;
                mjoint = tax;
            }   
            if(status==3 ) {
                tax =  8350*0.10 + (33950-8350)*0.15 + (income-33950)*0.25;
                mseperate= tax;
            }
            if(status ==4){
                tax = 11950*0.10 + (45500-11950)*0.15 +(income-45500)* 0.25;
                head =tax;
            }    
        }
    return (single);
    }
}

3 个答案:

答案 0 :(得分:1)

或者使用System.out.print()输出在computeTax内计算的每个值,然后执行System.out.println();以获得回车。

答案 1 :(得分:1)

您应该将所有计算结果滚动到他们自己的对象中并返回其中一个。

static class TaxDetails {

    double single = 0;
    double mjoint = 0;
    double mseperate = 0;
    double head = 0;
}

public static TaxDetails computetax(double income) {
    TaxDetails details = new TaxDetails();
    details.single = 8350 * .10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
    details.mjoint = 16700 * 0.10 + (income - 16700) * 0.15;
    details.mseperate = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
    details.head = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;
    return details;
}

public void test() {
    System.out.println("Taxable Income\t\t Single\t\t Married Joint\t\t MarriedSeerate\t\t HeadofaHouse");
    System.out.println("-----------------------------------------------------------------------------------------------------");
    for (double income = 50000; income <= 60000; income += 50) {
        TaxDetails tax = computetax(income);
        System.out.println(income + "\t\t" + tax.single + "\t\t" + tax.mjoint + "\t\t" + tax.mseperate + "\t\t" + tax.head);
    }
}

答案 2 :(得分:0)

您计算tax四次的值,然后丢弃该值并仅返回single的单个值。

将状态循环移出computetax并进入来电者。

编辑:鉴于您正在参加自学课程,这里的版本应该在您的限制范围内工作。

它仍然需要很多改进,但会让你更进一步。

public class TaxableIncome {

   public static void main(String[] args) {
       System.out.println("Taxable Income\t\t Single\t\t Married Joint\t\t MarriedSeerate\t\t HeadofaHouse");
       System.out.println("-----------------------------------------------------------------------------------------------------");
       double Tincome;
       int profile=1;
       for(Tincome=50000; Tincome<=60000; Tincome+=50) { 
           double single=computetax(1, Tincome);
           double joint=computetax(2, Tincome);
           double seperate=computetax(3, Tincome);
           double head=computetax(4, Tincome);
           System.out.println( Tincome +"\t\tsingle:\t" + single + "\tjoint:\t" + joint + "\tseparate:\t" + separate + "\thead:\t" + head);
       }
    }

    public  static double computetax( int status , double income) {
        double tax;
        double single=0;
        double mjoint=0;
        double mseperate=0;
        double head=0;

            if(status==1) {  
                tax = 8350*.10 + (33950-8350)*0.15 + (income- 33950)*0.25;
                single= tax;
            }
            if(status==2) {
                tax = 16700*0.10 + (income-16700)*0.15;
                mjoint = tax;
            }   
            if(status==3 ) {
                tax =  8350*0.10 + (33950-8350)*0.15 + (income-33950)*0.25;
                mseperate= tax;
            }
            if(status ==4){
                tax = 11950*0.10 + (45500-11950)*0.15 +(income-45500)* 0.25;
                head =tax;
            }    
        }
    return (single);
    }
}

public static void main(String[] args) { System.out.println("Taxable Income\t\t Single\t\t Married Joint\t\t MarriedSeerate\t\t HeadofaHouse"); System.out.println("-----------------------------------------------------------------------------------------------------"); double Tincome; int profile=1; for(Tincome=50000; Tincome<=60000; Tincome+=50) { double single=computetax(1, Tincome); double joint=computetax(2, Tincome); double seperate=computetax(3, Tincome); double head=computetax(4, Tincome); System.out.println( Tincome +"\t\tsingle:\t" + single + "\tjoint:\t" + joint + "\tseparate:\t" + separate + "\thead:\t" + head); } } public static double computetax( int status , double income) { double tax; double single=0; double mjoint=0; double mseperate=0; double head=0; if(status==1) { tax = 8350*.10 + (33950-8350)*0.15 + (income- 33950)*0.25; single= tax; } if(status==2) { tax = 16700*0.10 + (income-16700)*0.15; mjoint = tax; } if(status==3 ) { tax = 8350*0.10 + (33950-8350)*0.15 + (income-33950)*0.25; mseperate= tax; } if(status ==4){ tax = 11950*0.10 + (45500-11950)*0.15 +(income-45500)* 0.25; head =tax; } } return (single); } }