gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:12:29 27 4
gpt4 key购买 nike

我收到一个错误,指出我使用的变量之一未初始化,但在查看代码后,我意识到我实际上已经初始化了该变量,所以我不明白为什么我不断收到错误。

这是错误:

    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);
}
}

任何帮助将不胜感激。

最佳答案

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

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;
}
}

关于java - 使用 JCreator 的 Java 所得税程序出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21768390/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com