从一种方法到另一种方法携带双变量

时间:2016-10-19 13:30:18

标签: java variables methods

删除以避免剽窃...

3 个答案:

答案 0 :(得分:4)

Your problem is that your local variable tax inside your method is shadowing the outer static tax variable.

So your code updates that "inner" tax; but not the outer.

Thus: simply remove all those

double tax = 0;

statements from the body of your method!

But: the better approach would be that your method returns that value. There is not much sense in using global variables to "communicate" values; when your method could simply return them! So change the method from "void" to "double", and simply return the computed value!

Plus: you duplicated those print statements for no reason. Just do one print in the end of the method! Seriously: whenever you have two pieces of code that do exactly the same thing ... get rid of the second one somehow!

And finally: never ever use special chars such as $ for variable/method/... names in your code!

答案 1 :(得分:1)

double $$ = income - tax;//here tax is using your class static variable tax, which is never changed from 0.

To debug.. change your class variable to 1000 instead of 0 as a default value.

In your taxPayable() method dont declare double tax again. Just use the variable without declaring it

答案 2 :(得分:0)

because you are re-declaring variable tax - please read about scopes. change each double tax = ... in your method taxPayable(int income) to just tax.