Java中使用JCreator的所得税程序出错

Java中使用JCreator的所得税程序出错,java,jcreator,Java,Jcreator,我得到了一个错误,说我使用的一个变量没有初始化,但是在查看代码时,我意识到我实际上已经初始化了这个变量,所以我不明白为什么我一直得到这个错误 这就是错误: error: variable tax might not have been initialized avgtax = (tax/sal)*100; ^ 代码如下: import java.util.*; public class Taxable { final static double first15=

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

这就是错误:

    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);
  }
}
import java.util.*;
公共课应税{
最终静态双前15=0.1;
最终静态双next20=0.2;
最终静态双超35=0.25;
最终静态双重税=0.8;
公共静态void main(字符串[]args){
双sal,TaxInc;
双重税,平均税率;
扫描仪输入=新扫描仪(系统输入);
System.out.printf(“输入薪资:”);
sal=in.nextDouble();
TAXNC=sal*税务公司;
如果(TaxInc>0&&TaxInc 15000&&TaxInc 35000)
税=(15000*first15)+(20000*next20)+(TaxInc-35000)*超过35);
其他的
System.out.printf(“\n无效工资数字”);
avgtax=(税/sal)*100;
System.out.printf(“\n工资为:%3.2f\n要支付的税为:”
+“%3.2f\n平均税率为:%3.1f”,sal,Tax,avgtax);
}
}
任何帮助都将不胜感激

Tax is not initialized and the error is due to the if loops.
如果所有If都失败,
Tax
将不会获得值


只需将
双税=0

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

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);
    }
}
公共类{
最终静态双前15=0.1;
最终静态双next20=0.2;
最终静态双超35=0.25;
最终静态双重税=0.8;
公共静态void main(字符串[]args){
双瓣;
双taxInc;
双重税=0;
双avgtax;
扫描仪输入=新扫描仪(系统输入);
System.out.printf(“输入薪资:”);
sal=in.nextDouble();
TAXNC=sal*税务公司;
如果(taxInc>0和&taxInc 15000和&taxInc 35000)税=(15000*first15)+(20000*next20)+(taxInc-35000)*超过35);
else System.out.printf(“\n无效工资数字”);
avgtax=(税/sal)*100;
System.out.printf(“\n工资为:%3.2f\n应支付的税费为:%3.2f\n平均税率为:%3.1f”,sal,Tax,avgtax);
}
}
但是它的结构不是很好。。。我建议阅读Java命名约定和编程标准

编辑:

虽然您已经接受了答案,但我建议您看看这个代码示例,它是以更可读的方式编写的:)

import java.util.Scanner;
公共课应税{
私人最终静态双首15=0.1;
专用最终静态双next20=0.2;
私人最终静态双超过35=0.25;
私人最终静态双重税=0.8;
私人双萨尔;
私人双重征税;
私人双重税=0;
私人双avgtax;
公共静态void main(字符串[]args){
System.out.printf(“输入薪资:”);
扫描仪输入=新扫描仪(系统输入);
应税=新应税();
应纳税.setSal(在.nextDouble()中);
System.out.println(应纳税的.calculateAX());
}
私有字符串calculateTax(){
setTaxInc(getSal()*税务公司);
if(getTaxInc()>0&&getTaxInc()15000&&getTaxInc()35000){
setTax((15000*first15)+(20000*next20)+((getTax()-35000)*超过35));
}
否则{
System.out.printf(“\n无效工资数字”);
}
setAvgtax((getTax()/getSal())*100);
String calculation=“\n工资为:“+getSal()+”\n应缴纳的税款为:“+getTax()+”\n平均税率为:“+getAvgtax()”;
收益计算;
}
公共双getSal(){
返回sal;
}
公共无效设置(双sal){
this.sal=sal;
}
公共双getTaxInc(){
返回taxInc;
}
公共无效设置taxInc(双taxInc){
this.taxInc=taxInc;
}
公共双getTax(){
退税;
}
公共税(双重税){
这个。税=税;
}
公共双getAvgtax(){
返回avgtax;
}
公共无效setAvgtax(双avgtax){
this.avgtax=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;
    }
}