gpt4 book ai didi

Java 变量未设置

转载 作者:行者123 更新时间:2023-12-02 11:07:18 25 4
gpt4 key购买 nike

好吧,所以我试图让变量“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) 不能应用于 ()。有什么帮助吗?谢谢!谢谢!

最佳答案

totalTax = totalTax;
total = total;

尝试...

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

该方法内的局部变量对类顶部声明的实例变量具有支配地位,因此分配 totalTax = totalTax不执行任何操作 - 它将方法中的局部变量分配给自身。使用this.totalTax表示您想使用实例变量。

您可以在该方法中使用不同的变量名称来完全避免此类事情。

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

关于Java 变量未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5921848/

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