c - 让这个计数器更好地工作

标签 c button delay

if (iButton == 1)
    {
        for (int counter = 0; counter < 2; counter ++)
        {
            if(counter == 0)
            {
            currentTapRead = currentTapRead * 0.5;
            printf("1/2\n");
            }

            if(counter == 1)
            {
            currentTapRead = currentTapRead * 2;
            printf("1\n");
            }
        }
    }
}

嗨,大家好,我试图做到这样,当点击按钮时,它将除以 2,当再次点击按钮时,它会乘以 2,然后返回到开头。问题是当我按下按钮时它会同时执行这两个操作。因此什么也不做..我需要找到一种方法让计数器一次只累加1,而不是累加整个序列,但是你不能只将count + 1放入for循环中。

大家有什么想法吗?

最佳答案

您的代码不应处于循环中,而应仅在点击按钮时执行一次。计数器变量应该是静态的,以便它在每次点击之间保持其值。

关于c - 让这个计数器更好地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34107797/

相关文章:

python - Python serial readline 会中断 Arduino 循环吗?

c - C 中的 volatile 关键字

c - Main.c :3:9: error: expected ‘=’ , ‘,’ 、 ‘;’ 、 ‘asm’ 或 ‘__attribute__’ 位于 ‘{’ token 之前

javascript - 无法使用 getElementById 检索/显示按钮 ID

ios - 如何修复 UIPageViewController 页面中突出显示状态的 UIButton 延迟?

jQuery 添加和删除延迟

c - ALSA中period的含义

python - 如何制作一个用CFFI包裹的C-DLL来回调python

c# - 如何使用 GTK 更新 gtk.Button 中的图像#

css - mediaelement.js -> 更大的按钮?