这种方法是几种之一。我想知道,如果我在这里说“if(dig1.contains()& & dig2.contains()& res.contains())”是正确的。在正则表达式中 1.我需要确保字符串 中没有字母2.我需要查看string1和string2的总和是否合计为第三个字符串。否则返回false。感谢大家的帮助。这是迄今为止我所拥有的。如果没有字母则返回true
/**
Returns true if the puzzle is solved.
@return true if the puzzle has no letters and the
first two numbers add up to the third
*/
public boolean isSolved()
{
String dig1=""+add1;
String dig2=""+add2;
String res=""+result;
//String a1=""+dig1;
if(dig1.contains("[^A-Z]") && dig2.contains("[^A-Z]") && res.contains("[^A-Z]")){
int i=Integer.parseInt(dig1);
int j=Integer.parseInt(dig2);
int k=Integer.parseInt(res);
if(i+j==k)
return true;
else
return false;
}
else
return false;
}
如果字符串包含除数字和字母之外的其他符号,该怎么办?你会得到NumberFormatException。 – 2012-02-25 10:34:36
不,它是完全数字和字母。输入是这样的:add1 = SJF3G add2 = SDFH3 result = 3SH4H。没有符号或其他任何东西。感谢您的帮助。 – 2012-02-25 10:40:48
还有一件事。我注意到你倾向于这样做:'String dig1 =“”+ add1;'。如果你想连接字符串,只要'字符串dig1 + = add1;'或者如果你想分配一个字符串到另一个,做这个'字符串dig1 = add1;' – user219882 2012-02-25 11:51:57