我正在创建一个十六进制,十进制和二进制转换器,目前进展顺利。这是我在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