单击按钮后如何在for循环中打印序列号

编程入门 行业动态 更新时间:2024-10-12 01:21:56
本文介绍了单击按钮后如何在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循环中打印序列号

本文发布于:2023-10-28 00:04:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534923.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列号   单击   按钮   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!