否定值小于0

时间:2013-02-27 02:32:49

标签: android

我在if else语句中有这个代码...我试图阻止textview值低于0,但它仍然低于0。

else if(iv1.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.airplane2).getConstantState())){
                    int e =Integer.parseInt(textView5.getText().toString());
                    int f = e-1;
                    String s3 = String.valueOf(f);
                    textView5.setText(s3);
                    int dInt = Integer.parseInt(textView5.getText().toString());
                    if(dInt <= 0)
                                  {

                                       Toast.makeText(getApplicationContext(), "GameOver",Toast.LENGTH_SHORT).show();

                                  }

                }

P.S。对不起我的英语我会减少我的代码,但它仍然是负值

2 个答案:

答案 0 :(得分:0)

Math.max可以满足您的需求。

int b = Math.max(a - 2, 0);

答案 1 :(得分:0)

if  (iv1.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.airplane1).getConstantState())) {{
                    int a=Integer.parseInt(textView5.getText().toString());
                    int b=a+12;
                    String s1 = String.valueOf(b);
                    textView5.setText(s1);


                    Toast.makeText(getApplicationContext(), "Correct",
                            Toast.LENGTH_SHORT).show();
                }
                } int aInt = Integer.parseInt(textView5.getText().toString());
                 if(aInt <= 0) 
                 {
                     Toast.makeText(getApplicationContext(), "WrongWa",
                                Toast.LENGTH_SHORT).show();
                    } else if   (iv1.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.airplane2).getConstantState())){

                     int c=Integer.parseInt(textView5.getText().toString());
                         int  d=c-2;
                      String s1 = String.valueOf(d);
                     textView5.setText(s1);
                     {
                     Toast.makeText(getApplicationContext(), "Wrong",
                                Toast.LENGTH_SHORT).show();
                     }
                    }
            }


            });

我已经尝试了这个,它的实际工作方式我希望它成为@Pragnani感谢评论