0

我是 xamarin android 的初学者,我尝试在 xamarin android 中开发一个简单的应用程序,它只使递减值为零......但是当我尝试递减值继续为负值时,我不能让它停在零值

如何使递减值停止在零?

enter code here   public class MainActivity : AppCompatActivity
{
    int txt3;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        TextView textView1 = FindViewById<TextView>(Resource.Id.textView1); 
        txt3 = 5;
        textView1.Click += delegate
        {
            textView1.Text = (txt3--).ToString();

        };
    }
}

}

当我运行应用程序时,值为 5 4 3 2 1 0 -1 -2 -3 ...等

4

2 回答 2

0

在这里,您在不检查它是否低于 0 的情况下递减该值,这就是它变为负值的原因。你可以试试

if(0 < txt3)
{
   txt3 = txt3 - 1;
   textView1.Text = Convert.ToString(txt3);
}
于 2018-11-23T06:08:57.523 回答
0
  textView1.Click += delegate
{
    result = text3--;
    if (result < 0) {
        result = 0;
    }
    textView1.Text = (result).ToString();
};
于 2018-11-23T06:28:23.773 回答