gpt4 book ai didi

java - 如何将文本输入与多个字符串进行比较?

转载 作者:行者123 更新时间:2023-12-01 10:00:52 24 4
gpt4 key购买 nike

我希望以下 if 语句与多个字符串进行比较,但是当我与多个字符串进行比较时,它会显示我创建的错误消息。下面是不起作用的代码。

变量为 test = 'c3400553' 和 test2 = 'c3400554'

if (!uname.getText().toString().matches("[cC][0-9]{7}") ||
!uname.getText().toString().equals(test) ||
!uname.getText().toString().equals(test2)
) {
uname.setError("Incorrect ID Format");
}

下面是可用于一次比较的代码。

String test = "c3400553";
...

if (!uname.getText().toString().matches("[cC][0-9]{7}") ||
!uname.getText().toString().equals(test)
) {
uname.setError("Incorrect ID Format" );
}

我不明白问题是什么

最佳答案

这是因为您要么需要删除一些 !,要么需要将 || 替换为 &&

这取决于您想要实现的目标。如果您希望 id 在与格式不匹配且不等于 test 并且也不等于 test2 时被声明为不正确,那么解决方案是这:

if (!uname.getText().toString().matches("[cC][0-9]{7}") && 
!uname.getText().toString().equals(test) &&
!uname.getText().toString().equals(test2) ) {

uname.setError("Incorrect ID Format" );
}

否则,如果您想要检查 uname 是否与格式匹配,并且不等于 test 和 test2,那么问题是您需要在比较之前删除 !测试和测试2:

if (!uname.getText().toString().matches("[cC][0-9]{7}") || 
uname.getText().toString().equals(test) ||
uname.getText().toString().equals(test2) ) {

uname.setError("Incorrect ID Format" );
}

关于java - 如何将文本输入与多个字符串进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36822860/

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