Java变量未设置

时间:2011-05-07 15:01:24

标签: java

好吧所以我试图让变量“totalTax”和“total”保持项目的价格,当我在他们设置的方法中调用它们时它们工作正常,但当我从另一个类,它返回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 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]);
    }
 }
 }

然后,我正在运行这个使用Input类的类,它返回0,但是当我在已经拥有的同一个类中执行System.out.println时,它会工作并返回总数。

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

编辑:局部变量问题。我试图在第一个类中使用“return array(int AnyElement)”方法,但是当我尝试编译它时告诉我:Input中的returnArray(int)不能应用于()。有帮助吗?谢谢! 谢谢!

3 个答案:

答案 0 :(得分:8)

totalTax = totalTax;
total = total;

...试

this.totalTax = totalTax;
this.total = total;

该方法中的局部变量优先于在类顶部声明的实例变量,因此赋值totalTax = totalTax什么也不做 - 它将方法中的局部变量赋给自身。使用this.totalTax表示您要使用实例变量。

您可以在该方法中使用不同的变量名来完全避免这种情况。

(如果您不打算在该方法中创建新变量,请参阅MByD的答案。)

答案 1 :(得分:4)

此:

double totalTax=0.0;
double total=0.0;

在方法内部隐藏名为totalTaxtotal的类成员,在方法中使用它们时删除double

totalTax=0.0;
total=0.0;

答案 2 :(得分:1)

这是因为您设置了具有相同名称的本地变量。

totalTax=0.0;
total=0.0;