我试图存储的布尔值m.find()
出来是真的。我希望在程序"Successful"
为真时进行打印。我如何用if
语句检查布尔值是否正确?由于无法像示例代码中那样在字符串答案中存储布尔值,我将如何处理?
这是我到目前为止所拥有的。
Pattern p = Pattern.compile("(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]");
Matcher m = p.matcher("22:30");
System.out.println(m.find());
String answer = m.find();
if(answer==true){
System.out.println("Successful");
}
更新
public static void main(String[] args){
Pattern p = Pattern.compile("(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]");
Matcher m = p.matcher("22:30");
System.out.println(m.find());
if(m.find()){
System.out.println("Successful");
}
是的,但最好将a存储boolean
为boolean
。
if (m.find()) {
System.out.println("Successful");
}
或者
String answer = Boolean.toString(m.find());
if(answer.equals("true")){
System.out.println("Successful");
}
或者
String answer = m.find() ? "Successful" : "Unsuccessful";
System.out.println(answer);
但是您的模式只会匹配一次,因此您只能调用find()
一次。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句