-1

我有一個問題,我需要根據一些布爾值創建一些按鈕。編程問題布爾邏輯

如果button1 = true我應該創建button1,如果button2 = true我應該創建button2,如果button3 = true我應該創建button3。

所以可以有8種組合:

1 - button1, button2, button3 (button1 = true, button2 = true, button3 = true) 
2 - button1, button2 (button1 = true, button2 = true, button3 = false) 
3 - button1, button3 
4 - button2, button3 
5 - button2, button1 
6 - button1 
7 - button2 
8 - button3 

我的問題是如何找到正確的組合出8

+1

3個單獨的'if'條件? – 2013-04-29 06:38:13

+0

請用代碼顯示:) – 2013-04-29 06:39:45

+1

if(button1)createButton1;如果(button2)createButton2;等等。我不太瞭解你的問題。 – 2013-04-29 06:40:04

回答

0

不知道什麼真正的問題是:好像編程101 (或編程傻瓜第2頁)。最明顯的方法:

if (button1) { createButton1(); } 
if (button2) { createButton2(); } 
if (button3) { createButton3(); } 

如果組合是一個有點複雜,您可能需要單獨的語句:

if (button1 && button2 && button3) { 
    // create required buttons for case 1 
} else if (button1 && button2) { 
    // create required buttons for case 2 
} 
... 

的問題的情況下,訂單都ok - 你需要最具體到最具體(如果「只是button1」是第一個,它將「竊取」所有其他依賴於按鈕1的其他案例。)

另一種方法是將布爾值編碼爲int並使用開關。這可能是一個有點更具擴展性,如果你可能需要添加複雜的條件按鈕4 5 6

int choice = button1 ? 1 : 0; 
choice += button2 ? 2 : 0; 
choice += button3 ? 4 : 0; 
switch(choice) { 
    case 0: break; // no buttons 
    case 1: // just button1 
    case 2: // just button2 
    case 3: // button1 and button2 
    ... 
} 
1

如果按鈕1 =真我應該創建按鈕1,如果按鈕2 =真我,如果>創建BUTTON2 button3 = true我應該創建button3。

你似乎已經寫完整的僞代碼了。嘗試三個if語句。