gpt4 book ai didi

java - 在方法中传递 boolean 参数,但方法 boolean 变量仍然未使用

转载 作者:行者123 更新时间:2023-12-01 20:04:46 32 4
gpt4 key购买 nike

这里有点Java菜鸟。因此,我在处理类中调用一个名为 ageRestrProcessor() 的方法,该方法采用 boolean 值 ageValidationStatus 参数。 Processing 类有一个 boolean 变量 ageNotValidated 已初始化为 true。所以现在,我尝试将 ageNotValidated 变量作为参数传递到 ageRestrProcessor() 方法中,然后在该方法中尝试将 boolean 值更改为 false 。但是,在方法本身中,它表示 boolean 变量ageValidationStatus未使用。显然, boolean 值永远不会改变。从表面上看,这似乎应该可行,但我无法弄清楚问题是什么。预先感谢您的回复。

//ItemProcessors class

public class ItemsProcessors {
void ageRestrProcessor(int keyNum, String itemNameFromDB, double itemUnitPriceFromDB, boolean ageValidationStatus, boolean ageValidator, String itemUnit, Map<Integer, Object[]> map){
double ageRstrItemPrice = bp.roundUpToTwoDecimals(itemUnitPriceFromDB);
String dollarAgeRestPrice = bp.addDollarFormat(ageRstrItemPrice);

if (ageValidator){
System.out.println("\tAge Validation Successful");
System.out.println("\tTotal Price: " + dollarAgeRestPrice);
map.put(keyNum, new Object[] {itemNameFromDB, itemUnitPriceFromDB, itemUnit, ageRstrItemPrice, dollarAgeRestPrice});

//Here, the compiler is telling me that ageNotValidated variable is never used.
ageValidationStatus = false;
}
else {
System.out.println("\tShopper is underage. Item not added");
}
}
}

//I'm calling the ageRestrProcessor method into this Processing class

public class Processing extends OrderData {
private Map<Integer, Object[]> itemMap = new HashMap<Integer, Object[]>();
BasePage bp = new BasePage();
private Exceptions xcep = new Exceptions();
private ItemsProcessors ip = new ItemsProcessors();
public boolean ageNotValidated = true;

public void itemsProcessor(XSSFSheet itemSheet){
if (xcep.isAgeRestricted(itemSheet, rowNum) && ageNotValidated){
String ageEntered = bp.getStringFromScanner("\tEnter DOB in 'mm-dd-yyyy' format: ");
boolean isAgeValid = xcep.ageValidator(ageEntered);

//I'm trying to pass the ageNotValidated variable into the method here.
ip.ageRestrProcessor(keynum++, itemNameFromDB, itemUnitPriceFromDB, ageNotValidated, isAgeValid, itemUnit, itemMap);
}
}
}

最佳答案

ageValidationStatus 是在 ageRestrProcessor 方法内部创建的,不能在此方法之外使用(local 变量),并且不会在该方法中的其他任何地方使用当前方法也是如此

所以这是编译器发出的警告,以提高代码质量,当它在其他地方没有用时进行不必要的赋值

<小时/>

使ageRestrProcessor返回boolean标志并相应地使用值

  boolean ageRestrProcessor(int keyNum, String itemNameFromDB, double itemUnitPriceFromDB, boolean ageValidationStatus, boolean ageValidator, String itemUnit, Map<Integer, Object[]> map){
//..code
// you can remove boolean ageValidationStatus from method signature
if (ageValidator){
//..code
return false;
}
else {
System.out.println("\tShopper is underage. Item not added");
return true;
}
}

ageNotValidated = ip.ageRestrProcessor(keynum++,....;

关于java - 在方法中传递 boolean 参数,但方法 boolean 变量仍然未使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47599357/

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