Java 新的编码在获取此代码的输出时遇到问题
当我需要拿回一个值时,我不断地得到0作为税率和纳税额的输出。税率为毛利润的15%,即支付的税款总额乘以毛利润税率Java 新的编码在获取此代码的输出时遇到问题,java,Java,当我需要拿回一个值时,我不断地得到0作为税率和纳税额的输出。税率为毛利润的15%,即支付的税款总额乘以毛利润税率 import java.util.*; public class ProfitProjection{ double totalSales; double profitMargin; double projectedtotalSales; double grossProfit; double taxRate; double taxPa
import java.util.*;
public class ProfitProjection{
double totalSales;
double profitMargin;
double projectedtotalSales;
double grossProfit;
double taxRate;
double taxPaid;
double netProfit;
Scanner dataIn=new Scanner (System.in);
public ProfitProjection(){
System.out.print("Enter projected amount of total sales:");
totalSales=dataIn.nextDouble();
System.out.print("Enter the profit margin:");
profitMargin=dataIn.nextDouble();
taxRate = grossProfit * .15f;
grossProfit = totalSales * profitMargin;
netProfit = grossProfit - taxRate;
taxPaid = grossProfit * taxRate;
System.out.println("Projected total sales:" + totalSales);
System.out.println("Profit margin:" + profitMargin);
System.out.println("Estimated gross profit:" + grossProfit);
System.out.println("Tax rate:" + taxRate);
System.out.println("Amount of tax paid:" + taxPaid);
System.out.println("Estimated net profit:" + netProfit);
}
public static void main(String args[]){
new ProfitProjection();
}
}
您只声明了
grossProfit
,没有给它赋值,默认为0.0
public ProfitProjection(){
grossProfit = 1.0; // assign value to it in your constructor
taxRate = grossProfit * .15f;
}
在这方面:
taxRate=grossProfit*.15f代码>
您从未初始化过grossProfit
,只是声明了它。因此,该值被视为0,因此taxRate
被设置为0 你的问题是,你没有在设定税率之前设定毛利润。因此,上面的答案是稍微正确的,但不是100%,我将向您展示:
public class ProfitProjection{
double totalSales;
double profitMargin;
double projectedtotalSales;
double grossProfit;
double taxRate;
double taxPaid;
double netProfit;
Scanner dataIn=new Scanner (System.in);
public ProfitProjection(){
System.out.print("Enter projected amount of total sales:");
totalSales=dataIn.nextDouble();
System.out.print("Enter the profit margin:");
profitMargin=dataIn.nextDouble();
grossProfit = totalSales * profitMargin; //Swapped this line
taxRate = grossProfit * .15f; //With this line to have grossProfit set at a value before multiplication.
netProfit = grossProfit - taxRate;
taxPaid = grossProfit * taxRate;
System.out.println("Projected total sales:" + totalSales);
System.out.println("Profit margin:" + profitMargin);
System.out.println("Estimated gross profit:" + grossProfit);
System.out.println("Tax rate:" + taxRate);
System.out.println("Amount of tax paid:" + taxPaid);
System.out.println("Estimated net profit:" + netProfit);
}
public static void main(String args[]){
new ProfitProjection();
}
}
希望这有帮助
编辑:
我在上面代码中所做的更改是我交换了grossProfit的设置和taxRate的设置。我还在这些行上添加了注释。您的计算顺序错误
您使用grossProfit
的值来计算taxRate
计算grossProfit
的值
问题是,在第一步中,如果尚未计算实际的grossProfit
值,则其值将为0.0
。这是由于Java的默认初始化规则适用于类的所有字段
如果字段未显式初始化,或者您可以(以某种方式)看到其
值,然后您将看到
默认初始值
false
对于布尔值
- 零表示任何其他基元类型
null
用于任何引用类型
简而言之,你的计算顺序是错误的
解决方案:更改这两条语句的顺序。。。检查你在其他地方没有犯同样的错误
事实上,还有一个重要的错误(也许)。。。导致这一个没有被检测到
您将这些变量声明为实例字段。考虑到目前编写的代码,它们可以被声明为局部变量。如果您这样做了,那么编译器会在初始化之前告诉您正在使用grossProfit
我说这可能是一个错误,因为这取决于是否有更多的代码/功能需要稍后添加
还有几个其他错误:
taxRate
变量命名错误。税率为0.015华氏度。该变量包含的是实际应付税款,而不是税率
import java.util.*;
public class ProfitProjection{
double totalSales;
double profitMargin;
double projectedtotalSales;
double grossProfit;
double taxRate;
double taxPaid;
double netProfit;
Scanner dataIn=new Scanner (System.in);
public ProfitProjection(){
System.out.print("Enter projected amount of total sales:");
totalSales=dataIn.nextDouble();
System.out.print("Enter the profit margin:");
profitMargin=dataIn.nextDouble();
taxRate = grossProfit * .15f;
grossProfit = totalSales * profitMargin;
netProfit = grossProfit - taxRate;
taxPaid = grossProfit * taxRate;
System.out.println("Projected total sales:" + totalSales);
System.out.println("Profit margin:" + profitMargin);
System.out.println("Estimated gross profit:" + grossProfit);
System.out.println("Tax rate:" + taxRate);
System.out.println("Amount of tax paid:" + taxPaid);
System.out.println("Estimated net profit:" + netProfit);
}
public static void main(String args[]){
new ProfitProjection();
}
}
您应该尽可能准确地指定税率。使用0.015
,而不是0.015 f
。eff表单是一个float
literal而不是double
literal
你没有在任何地方设置grossrofit
。(0*任何东西)
是0。看看你的grossrofit
。他设置了grossrofit
。他将其声明为双精度,然后将其设置为totalSales*profitMargin
他设置了grossProfit。他将其声明为双精度,然后将其设置为totalSales*profitMargin@JoshHeaps OP在使用前一行后进行设置。是的,是的,他这样做了。我在回答中提到了这一点。他设置了grossProfit
。他将其声明为双倍,然后将其设置为totalSales*profitMargin@JoshHeaps,位于taxRate=grossProfit*.15f之后
OP说他总是在taxRate
@JoshHeaps中得到0,我更新的编辑添加了taxRate=grossProfit*.15f代码>这可以帮助你更好地理解这个答案。指导方针明确说明了你的编辑以及为什么要进行编辑。正如S.O.指南所说,请留下评论,这样答案可能会有所改进。我不是反对者,但显然你的答案对反对者来说并不清楚。我很难理解您修改了什么感谢您的更正,它帮助我理解了我的问题,现在它输出了正确的值,谢谢。