我是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。。。etc
textView1.Click += delegate
{
result = text3--;
if (result < 0) {
result = 0;
}
textView1.Text = (result).ToString();
};
在这里,您正在递减该值,而不检查它是否低于0,这就是为什么它会变成负值。你可以试试
if(0 < txt3)
{
txt3 = txt3 - 1;
textView1.Text = Convert.ToString(txt3);
}