gpt4 book ai didi

java - 字符串条件检查测试失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:15 26 4
gpt4 key购买 nike

我有一个验证石斑鱼字符串的方法:

public String validateGrouper(String grouper)
{
if(grouper!=null)
grouper = grouper.trim();
if(grouper==null || grouper.equals("") || !grouper.equalsIgnoreCase("AND") || !grouper.equalsIgnoreCase("OR"))
{
System.out.println("Trap for grouper validation triggered with grouper: "+grouper);
grouper="AND";
}
else
grouper = grouper.toUpperCase();
return grouper;
}

我正在针对此测试运行它:

@Test
public void testValidateGrouper(){
DBUtils dbUtils = new DBUtils();
assertEquals("Expect empty string to be replaced by AND","AND",dbUtils.validateGrouper(""));
assertEquals("Expect spaced out string to be replaced by AND","AND",dbUtils.validateGrouper(" "));
assertEquals("Expect the lower case and to be used as AND","AND",dbUtils.validateGrouper("and"));
assertEquals("Expect the upper case AND to be used as AND","AND",dbUtils.validateGrouper("AND"));
assertEquals("Expect the word CART to be ignored and replaced by AND","AND",dbUtils.validateGrouper("CART"));
assertEquals("Expect AND to be used as the grouper if no grouper is provided","AND",dbUtils.validateGrouper(null));
assertEquals("Expect the lower case or to be used as OR","OR",dbUtils.validateGrouper("or"));
assertEquals("Expect the upper case OR to be used as OR","OR",dbUtils.validateGrouper("OR"));
}

我发现尽管使用了 ANDOR,但条件总是被触发。我不明白为什么条件失败。

最佳答案

如果允许 AND 或 OR,则需要

if(grouper==null || !(grouper.equalsIgnoreCase("AND") || grouper.equalsIgnoreCase("OR")))

否则,如果 grouper 为“OR”,则不等于“AND”且条件为真,如果 grouper 为“AND”,则不等于“OR”且条件为真。

此外,您还可以删除 grouper.equals("") 检查。

关于java - 字符串条件检查测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30639369/

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