在比较EditText的输入整数时遇到问题。我无法找到它的错误。请帮我。这是下面的代码。
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.problem2);
textIn = (EditText) findViewById(R.id.probText);
Button ans3 = (Button) findViewById(R.id.answer3);
ans3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String probString = textIn.getText().toString();
Integer probInt = Integer.parseInt(probString);
Integer prob = 31;
if (probInt.equals(prob)) {
Toast toast = Toast.makeText(answer3.this,"CORRECT!",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
startActivity(new Intent("com.sample.androidsample.ANSWER4") );
} else {
Toast toast = Toast.makeText(answer3.this,"Wrong answer! Try again.",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
}
});
}
答案 0 :(得分:1)
试试这个:
Integer probInt = Integer.parseInt(probString);
Integer prob = 31;
//changed from equals() to ==
if (probInt == prob)) {
Toast toast = Toast.makeText(answer3.this,"CORRECT!",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
startActivity(new Intent("com.sample.androidsample.ANSWER4") );
} else {
Toast toast = Toast.makeText(answer3.this,"Wrong answer! Try again.",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
根据我的理解equals
是字符串。
答案 1 :(得分:0)
您可以使用Integer probInt = Integer.parseInt(probString);
代替Integer probInt = Integer.valueOf(probString);
,但我不确定这是否是问题。而且probInt.equals(prob)
代替probInt.equalIgnoreCase(prob)
,您可以使用{{1}}。
答案 2 :(得分:0)
我认为这是错误的。
Integer prob = 31;
这里Integer是一个类,所以你必须像下面那样实例化它。
Integer prob = new Integer(31);
或 你可以使用
int prob =31;
答案 3 :(得分:0)
而不是
Integer probInt = Integer.parseInt(probString);
Integer prob = 31;
if (probInt.equals(prob)) {
} else {
}
使用
int probInt = (int) Integer.parseInt(probString);
int prob = 31;
if (probInt == prob) {
} else {
}
答案 4 :(得分:0)
尝试这件事,它会起作用
Integer probInt = Integer.parseInt(probString);
Integer prob = 31;
//changed from equals() to ==
if (probInt.intValue() == prob.intValue()) {
Toast toast = Toast.makeText(answer3.this,"CORRECT!",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
startActivity(new Intent("com.sample.androidsample.ANSWER4") );
} else {
Toast toast = Toast.makeText(answer3.this,"Wrong answer! Try again.",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
答案 5 :(得分:0)
以下是清理输入的示例。为简洁起见省略了异常处理。
private Pattern patternNum = Pattern.compile("^(\\d{1,5})$",
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
// Wrap it in a try/catch for PatternSyntaxException!
private boolean validateNum(String inputNum){
return patternNum.matcher(inputNum);
}
然后假设validateNum
例程返回true,意味着它匹配至少5位数,然后这样说:
if (validateNum(probString)){
int probInt = Integer.parseInt(probString);
if (probInt == prob){
// Success
}else{
// Failure
}
}else{
// Whoops! Bad input caught!
}