我正在創建一個十六進制,十進制和二進制轉換器,目前進展順利。這是我在iPhone上的第二個項目,我是初學者。但是,我想知道如何簡化我的(一堆if語句)。我有:如何簡化我的程序和所有if語句?
if (entered is hex)
if (binary button clicked)
convert to binary
if (decimal button clicked)
convert to decimal
else (hex button clicked)
keep in hex and inform
else if (entered is binary)
if (hex button clicked)
convert to hex
if (decimal button clicked)
convert to decimal
else (binary button clicked)
keep in binary and inform user
else if (entered is decimal)
if (hex button clicked)
convert to binary
if (binary button clicked)
convert to hex
else (decimal button clicked)
keep in decimal and inform user
else
give error if something else entered in
這看起來很重複我。所有這些都屬於同一個班級,所有這些如果陳述彼此非常相似,所以我想知道我能做些什麼嗎?
謝謝你的時間。
什麼是 「六角鍵點擊」? – user523234 2013-02-16 03:05:39
你如何確定輸入是什麼類型?這是基於點擊按鈕還是哪個文本字段用於輸入?還有別的嗎? – rdelmar 2013-02-16 03:48:50
你的代碼已經足夠好讀。不需要額外的代碼分解功能,但可以增加可讀性。 – 2013-02-16 04:47:45