本文介绍了单击按钮后如何在for循环中打印序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨。 我有一个按钮,在Messagebox中显示i的值。我希望第一次显示值为1,然后退出循环, 然后再次点击按钮它应显示为2 并退出循环 然后再次点击按钮它应该显示为3 并退出循环 然后再次点击按钮它应显示为4 并退出循环 私人 void Button_Click( object sender,RoutedEventArgs e) {c = 1 ; for (i = 1 ; i < ; = 26 ; i ++) { // b = i; c = i; MessageBox.Show( i的值是 + i); }
如何更改此代码才能获得...
解决方案你不需要循环,只需将变量移到方法之外:
private int counter = 1 ; private void Button_Click( object sender,RoutedEventArgs e) { MessageBox.Show( counter的值是 + counter ++); }
hi . i have a button which displays the value of "i" in Messagebox. i want to display the value as 1 for 1sttime, and exit the loop, then for button click again it should display as 2 and exit the loop then for button click again it should display as 3 and exit the loop then for button click again it should display as 4 and exit the loop
private void Button_Click(object sender, RoutedEventArgs e) { c = 1; for (i = 1; i <= 26; i++) { // b = i; c = i; MessageBox.Show(" value of i is " + i); }how should i change this code to get that..
解决方案 You don't want a loop, just move the variable outside the method: private int counter = 1; private void Button_Click(object sender, RoutedEventArgs e) { MessageBox.Show("The value of counter is " + counter++); }更多推荐
单击按钮后如何在for循环中打印序列号
发布评论