Alright so I'm trying to have the variables "totalTax" and "total" hold the price of the item, and they work fine when I call them within the method they are being set at, but when I call them from another class, it returns 0.
public class Input
{
private Scanner keybd;
private String[] costArray;
private String[] itemArray;
private double totalTax;
private double total;
/**
* Constructor for objects of class Scanner
*
* @param开发者_如何学Python anyAmountofItems the amount of items you are going to be buying
*/
public Input(int anyAmountofItems)
{
keybd = new Scanner(System.in);
costArray = new String[anyAmountofItems];
itemArray = new String[anyAmountofItems];
totalTax = 0.0;
total = 0.0;
}
/**
* Mutator method to set the item names and costs
*
* @param anyValue the tax rate percentage (ex. 0.08 for 8%)
*/
public void setArray(double anyValue){
//System.out.println("Enter the sales tax percentage: ");
//double salesTax = keybd.nextDouble();
//for(int index=0; index < itemArray.length; index++){
//System.out.println("Enter the item name: ");
//itemArray[index] = keybd.next();}
double totalTax=0.0;
double total=0.0;
for(int indexc=0; indexc < costArray.length; indexc++){
System.out.println("Enter the item name: ");
String anyName = keybd.next();
itemArray[indexc] = anyName;
System.out.println("Enter the item cost: ");
double cost = Double.valueOf(keybd.next()).doubleValue();
costArray[indexc] = " " + cost;
totalTax = totalTax + (cost * anyValue);
total = total + cost;
}
//for(int indexa=0; indexa < itemArray.length; indexa++){
//System.out.println(itemArray[indexa] + "-" + costArray[indexa]);}
//System.out.println("Sales Receipt");
//System.out.println("--------------");
//for(int i=0;i<costArray.length;i++){
// System.out.println(itemArray[i] + " - $" + costArray[i]);
//}
//returnCostArray();
//System.out.println("Total tax: $" + totalTax);
//System.out.println("Total cost pre-tax: $" + total);
//System.out.println("Total cost including tax: $" + (total+totalTax));
totalTax = totalTax;
total = total;
}
public String returnArray(int anyElement){
int index = anyElement - 1;
return itemArray[index];
}
public double returnTotal(){
return total;
}
public double returnTotalTax(){
return totalTax;
}
public void returnCostArray(){
for(int i=0;i<costArray.length;i++){
System.out.println(itemArray[i] + " - $" + costArray[i]);
}
}
}
Then, I'm running this class which uses the Input class and it is returning 0, yet when I do the System.out.println in the same class that I already have, it works and returns the total.
public class TaxClass
{
private Input newList;
/**
* Constructor for objects of class Tax
*
* @param anyAmount Enter the number of items
*/
public TaxClass(int anyAmount)
{
newList = new Input(anyAmount);
}
/**
* Mutator method to add items and their cost
*
*
* @param anyTax Enter the sales tax percentage
*/
public void addItems(double anyTax){
double salesTax = anyTax;
newList.setArray(salesTax);
System.out.println("Sales Receipt");
System.out.println("--------------");
newList.returnCostArray();
System.out.println("Total tax: $" + newList.returnTotalTax());
System.out.println("Total cost pre-tax: $" + newList.returnTotal());
System.out.println("Total cost including tax: $" + (newList.returnTotal()+newList.returnTotalTax()));
}
//public String returnArray(){
//newList.returnArray();
//}
}
EDIT: Local variable problem. I'm trying to use the "return array(int AnyElement)" method in the first class, but when I try to compile it is telling me that: returnArray(int) in Input cannot be applied to (). Any help? Thanks! Thanks!
totalTax = totalTax;
total = total;
Try...
this.totalTax = totalTax;
this.total = total;
The local variable inside that method takes dominance over the instance variable declared at the top of the class, so assigning totalTax = totalTax
does nothing - it assigns the local variable in the method to itself. Using this.totalTax
indicates that you want to use the instance variable instead.
You can use different variable names inside that method to avoid this sort of thing entirely.
(If you didn't intend to create new variables inside that method, refer to MByD's answer.)
This:
double totalTax=0.0;
double total=0.0;
inside of a method shadows the class members called totalTax
and total
, remove the double
when you use them inside a method:
totalTax=0.0;
total=0.0;
That's because you are setting local variables with the same name.
try
totalTax=0.0;
total=0.0;
精彩评论