我有if else语句的代码
@Override
public void onClick(View arg0) {
int aInt = Integer.parseInt(textView1.getText().toString());
if(aInt <= 0) {
Toast.makeText(getApplicationContext(), "Wrong",
Toast.LENGTH_SHORT).show();
} else{
int a=Integer.parseInt(textView1.getText().toString());
int b=a-2;
String s1 = String.valueOf(b);
textView1.setText(s1);
Toast.makeText(getApplicationContext(), "Wrong",
Toast.LENGTH_SHORT).show();
int c =Integer.parseInt(textView5.getText().toString());
int d = c-1;
String s2 = String.valueOf(d);
textView5.setText(s2);
} if(textView5 != null) {
int dInt = Integer.parseInt(textView5.getText().toString());
if(dInt <= 0);
Toast.makeText(getApplicationContext(), "Game Over",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(MainActivity.this, Fifth.class);
startActivity(intent);
SavePreferences("MEM1", textView1.getText().toString());
}
}
};
在代码的这一部分,如果textView5的值为0,我想启动intent 但它仍然符合新的意图,即使textView5的价值还不是0我不知道我的代码的哪一部分是错的:
} if(textView5 != null) {
int dInt = Integer.parseInt(textView5.getText().toString());
if(dInt <= 0);
Toast.makeText(getApplicationContext(), "Game Over",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(MainActivity.this, Fifth.class);
startActivity(intent);
}
答案 0 :(得分:0)
if(textView5 != null)
{
int dInt = Integer.parseInt(textView5.getText().toString());
if(dInt <= 0)
{
Toast.makeText(getApplicationContext(), "Game Over",Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Fifth.class));
}
}
答案 1 :(得分:0)
这样做:
} if(textView5 != null) {
int dInt = Integer.parseInt(textView5.getText().toString());
if(dInt <= 0) {
Toast.makeText(getApplicationContext(), "Game Over",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(MainActivity.this, Fifth.class);
startActivity(intent);
}
}
答案 2 :(得分:0)
语句(textView5!= null)检查视图是否为空。您需要添加编辑代码,如。
if(!textView5.getText().toString().equals("")) {
int dInt = Integer.parseInt(textView5.getText().toString());
if(dInt <= 0);
Toast.makeText(getApplicationContext(), "Game Over",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(MainActivity.this, Fifth.class);
startActivity(intent);
}
答案 3 :(得分:0)
试试这个:if循环没有像这样的半冒号
if(dInt <= 0){
Toast.makeText(getApplicationContext(), "Game Over",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(MainActivity.this, Fifth.class);
startActivity(intent);
}
答案 4 :(得分:-1)
;
是String的结尾。如果你在If条件结束时给;
,那么正文就不会执行..
所以删除它并检查你的代码..
if(dInt <= 0)
Toast.makeText(getApplicationContext(), "Game Over",Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
......