使用JCreator的Java中的所得税计划出错

时间:2014-02-14 00:13:11

标签: java jcreator

我收到一个错误,说我使用的其中一个变量没有初始化,但在查看代码时我意识到我实际上已初始化变量,所以我不明白为什么我一直收到错误。

这是错误:

    error: variable tax might not have been initialized
avgtax = (tax/sal)*100;
          ^

这是代码:

import java.util.*;
public class Taxable {
  final static double first15=0.1;
  final static double next20=0.2;
  final static double over35=0.25;
  final static double tax_inc=0.8;
  public static void main(String[] args) {
    double sal, TaxInc;
    double tax, avgtax;
    Scanner in = new Scanner(System.in);
    System.out.printf("Enter Salary: ");
    sal = in.nextDouble();
    TaxInc = sal*tax_inc;
    if (TaxInc > 0 && TaxInc <= 15000)
      tax = TaxInc*first15;
    else if (TaxInc > 15000 && TaxInc <= 35000)
      tax = (15000 * first15) + ((TaxInc - 15000) * next20);
    else if (TaxInc > 35000)
      tax = (15000 * first15) + (20000 * next20) + ((TaxInc - 35000) * over35);
    else
      System.out.printf("\nInvalid Salary Figure.");
    avgtax = (tax/sal)*100;
    System.out.printf("\nAt a salary of: %3.2f\nTax to be paid is:"
      + " %3.2f\nAverage Tax Rate is: %3.1f", sal, tax, avgtax);
  }
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

这对您的代码有一些修复,因此它可以按预期编译和运行

public class Taxable {
    final static double first15=0.1;

    final static double next20=0.2;
    final static double over35=0.25;
    final static double tax_inc=0.8;
    public static void main(String[] args) {
        double sal;
        double taxInc;
        double tax = 0;
        double avgtax;
        Scanner in = new Scanner(System.in);
        System.out.printf("Enter Salary: ");
        sal = in.nextDouble();
        taxInc = sal*tax_inc;
        if (taxInc > 0 && taxInc <= 15000) tax = taxInc*first15;
        else if (taxInc > 15000 && taxInc <= 35000) tax = (15000 * first15) + ((taxInc - 15000) * next20);
        else if (taxInc > 35000) tax = (15000 * first15) + (20000 * next20) + ((taxInc - 35000) * over35);
        else System.out.printf("\nInvalid Salary Figure.");
        avgtax = (tax/sal)*100;
        System.out.printf("\nAt a salary of: %3.2f\nTax to be paid is: %3.2f\nAverage Tax Rate is: %3.1f", sal, tax, avgtax);
    }
}

然而,它结构不合理......我建议阅读Java命名约定和编程标准

编辑:

虽然您已经接受了答案,但我建议您查看此代码示例,其编写方式稍微有点可读:)

import java.util.Scanner;


public class Taxable {
    private final static double first15 =0.1;   
    private final static double next20  =0.2;
    private final static double over35  =0.25;
    private final static double tax_inc =0.8;

    private double sal;

    private double taxInc;
    private double tax = 0;
    private double avgtax;
    public static void main(String[] args) {
        System.out.printf("Enter Salary: ");
        Scanner in = new Scanner(System.in);

        Taxable taxable = new Taxable();
        taxable.setSal(in.nextDouble());
        System.out.println(taxable.calculateTax());
    }
    private String calculateTax(){  
        setTaxInc(getSal()*tax_inc);

        if (getTaxInc() > 0 && getTaxInc() <= 15000){
            setTax(getTaxInc()*first15);
        }
        else if (getTaxInc() > 15000 && getTaxInc() <= 35000){
            setTax((15000 * first15) + ((getTax() - 15000) * next20));
        }
        else if (getTaxInc() > 35000){
            setTax((15000 * first15) + (20000 * next20) + ((getTax() - 35000) * over35)) ;
        }
        else {
            System.out.printf("\nInvalid Salary Figure.");
        }
        setAvgtax((getTax()/getSal())*100);
        String calculation = "\n At a salary of: " + getSal() + "\n Tax to be paid is: " + getTax() + "\n Average Tax Rate is: " + getAvgtax(); 
        return calculation;     
    }
    public double getSal() {
        return sal;
    }
    public void setSal(double sal) {
        this.sal = sal;
    }
    public double getTaxInc() {
        return taxInc;
    }
    public void setTaxInc(double taxInc) {
        this.taxInc = taxInc;
    }
    public double getTax() {
        return tax;
    }
    public void setTax(double tax) {
        this.tax = tax;
    }
    public double getAvgtax() {
        return avgtax;
    }
    public void setAvgtax(double avgtax) {
        this.avgtax = avgtax;
    }
}

答案 1 :(得分:0)

Tax is not initialized and the error is due to the if loops.

如果一切都失败,Tax将无法获得价值。

刚刚放double Tax = 0;