值相等后的“复位”开关语句

时间:2018-12-10 18:58:17

标签: c# if-statement switch-statement

我想在总共单击3次按钮之后再次运行开关和if / else语句。

当前按下的代码用于三个按钮,每个按钮都有1个值。如果这些(当前按下的代码)等于包含3值的全局字符串,则图片框颜色将变为森林绿色。这是我的代码:

ListIterator

1 个答案:

答案 0 :(得分:0)

您可以通过多种方法来完成此任务,但是要尽量保持逻辑完整:

#include <stdexcept>

struct freqHz
{
    uint32_t value;
    constexpr explicit freqHz(uint32_t freq) : value(freq) {}
};

constexpr inline freqHz operator""_Hz(unsigned long long freq)
{
    if (freq >= (1ULL << 32)) {
        throw std::logic_error("Frequency Hz will overflow");
    }
    return freqHz(freq);
}

我所做的唯一更改是:

  1. 移动“错误密码”,否则在“正确密码”之后阻止 如果阻止
  2. 重置switch ((sender as Button).Text) { case "1": serialMonitor.PrintLine("1"); currentPressedCode = currentPressedCode + "1"; break; case "2": serialMonitor.PrintLine("2"); currentPressedCode = currentPressedCode + "2"; break; case "3": serialMonitor.PrintLine("3"); currentPressedCode = currentPressedCode + "3"; break; default: break; } if (buttonsPressed == 3) { if (currentPressedCode == vaultCode) { //vault open serialMonitor.PrintLine("vault"); pcbGreen.BackColor = Color.ForestGreen; } else { // wrong code serialMonitor.PrintLine("wrong"); MessageBox.Show("Wrong password"); // wrong password messagebox pcbRed.BackColor = Color.DarkRed; } buttonsPressed = 0; currentPressedCode = ""; } buttonsPressed 有人输入3个数字后