这是我的Android应用程序单位转换器。我有三个微调器:单元,从和到。 例如。角度,度数和弧度。
我为单位微调器添加了一个监听器。选择单位后,从和到微调器将被填充。用户将在中输入 EditText中的输入,按下Calculate按钮后,TextView将包含答案。
我使用if else
实现了这一点。
if unit_spinner is Angle
if from_spinner is Degree
if to_spinner is Radian
return input*0.0174532925 //1 degree = 0.0174532925 rad
else if to_spinner is Gradian
return input*1.111111111111111 //1 degree = 1.111111111111111 grad
...and so on, the cartesian product of all units
对于多个单位来说这变得非常长。所以你能提出另一种逻辑吗?
答案 0 :(得分:1)
根据您的变量,您可以使用switch
语句。
switch(someintegervariable){
case SOME_INT_CONSTANT_1:
/* ... */
break;
case SOME_INT_CONSTANT_2:
/* ... */
break;
default:
/* ... */
}
答案 1 :(得分:1)
现在的方式,对于每个有N个单位的类别,你有两个NxN if
语句。
if from_spinner is Degree
if to_spinner is Degree
return input
if to_spinner is Radian
return input * 0.0174532925199
if to_spinner is Gradian
return input * 1.11111111111
if from_spinner is Radian
if to_spinner is Degree
return input * 57.2957795131
if to_spinner is Radian
return input
if to_spinner is Gradian
return input * 63.6619772368
if from_spinner is Gradian
if to_spinner is Degree
return input * 0.9
if to_spinner is Radian
return input * 0.0157079632679
if to_spinner is Gradian
return input
相反,选择一个单元作为输入和输出之间的中介。然后你需要N if
个语句从输入转换为中间语,N if
语句从中间转换为输出,总共2N。
//we will use degrees as the intermediary unit
intermediary = null
//caluclate intermediary
if from_spinner is Degree
intermediary = input
if from_spinner is Radian
intermediary = input * 57.2957795131
if from_spinner is Gradian
intermediary = input * 0.9
//calculate final
if to_spinner is Degree
return intermediary
if to_spinner is Radian
return intermediary / 57.2957795131
if to_spinner is Gradian
return intermediary / 0.9
当你只有三个单位时,它看起来效率不高,但是对于较大的N值,它可以为你节省很多精力。例如,将这个105行双重嵌套方法与使用中间值的29行等效方法进行比较:
if from_spinner is Millimeter
if to_spinner is Millimeter
return input
if to_spinner is Centimeter
return input * 0.1
if to_spinner is Meter
return input * 0.001
if to_spinner is Kilometer
return input * 1e-06
if to_spinner is Inch
return input * 0.0393700787402
if to_spinner is Foot
return input * 0.00328083989501
if to_spinner is Mile
return input * 6.2137273665e-07
if from_spinner is Centimeter
if to_spinner is Millimeter
return input * 10.0
if to_spinner is Centimeter
return input
if to_spinner is Meter
return input * 0.01
if to_spinner is Kilometer
return input * 1e-05
if to_spinner is Inch
return input * 0.393700787402
if to_spinner is Foot
return input * 0.0328083989501
if to_spinner is Mile
return input * 6.2137273665e-06
if from_spinner is Meter
if to_spinner is Millimeter
return input * 1000.0
if to_spinner is Centimeter
return input * 100.0
if to_spinner is Meter
return input
if to_spinner is Kilometer
return input * 0.001
if to_spinner is Inch
return input * 39.3700787402
if to_spinner is Foot
return input * 3.28083989501
if to_spinner is Mile
return input * 0.00062137273665
if from_spinner is Kilometer
if to_spinner is Millimeter
return input * 1000000.0
if to_spinner is Centimeter
return input * 100000.0
if to_spinner is Meter
return input * 1000.0
if to_spinner is Kilometer
return input
if to_spinner is Inch
return input * 39370.0787402
if to_spinner is Foot
return input * 3280.83989501
if to_spinner is Mile
return input * 0.62137273665
if from_spinner is Inch
if to_spinner is Millimeter
return input * 25.4
if to_spinner is Centimeter
return input * 2.54
if to_spinner is Meter
return input * 0.0254
if to_spinner is Kilometer
return input * 2.54e-05
if to_spinner is Inch
return input
if to_spinner is Foot
return input * 0.0833333333333
if to_spinner is Mile
return input * 1.57828675109e-05
if from_spinner is Foot
if to_spinner is Millimeter
return input * 304.8
if to_spinner is Centimeter
return input * 30.48
if to_spinner is Meter
return input * 0.3048
if to_spinner is Kilometer
return input * 0.0003048
if to_spinner is Inch
return input * 12.0
if to_spinner is Foot
return input
if to_spinner is Mile
return input * 0.000189394410131
if from_spinner is Mile
if to_spinner is Millimeter
return input * 1609340.0
if to_spinner is Centimeter
return input * 160934.0
if to_spinner is Meter
return input * 1609.34
if to_spinner is Kilometer
return input * 1.60934
if to_spinner is Inch
return input * 63359.8425197
if to_spinner is Foot
return input * 5279.98687664
if to_spinner is Mile
return input
intermediary = null
if from_spinner is Millimeter
intermediary = input * 0.001
if from_spinner is Centimeter
intermediary = input * 0.01
if from_spinner is Meter
intermediary = input * 1.0
if from_spinner is Kilometer
intermediary = input * 1000.0
if from_spinner is Inch
intermediary = input * 0.0254
if from_spinner is Foot
intermediary = input * 0.3048
if from_spinner is Mile
intermediary = input * 1609.34
if to_spinner is Millimeter
return intermediary / 0.001
if to_spinner is Centimeter
return intermediary / 0.01
if to_spinner is Meter
return intermediary / 1.0
if to_spinner is Kilometer
return intermediary / 1000.0
if to_spinner is Inch
return intermediary / 0.0254
if to_spinner is Foot
return intermediary / 0.3048
if to_spinner is Mile
return intermediary / 1609.34
答案 2 :(得分:0)
我建议使用switch/case
语句。有了这个,您可以为每种单位类型设置一个案例,然后进行计算。
您还可以为每个计算编写方法。这可能会缩短一点,但可能不会太多。
我只想使用case
声明。它相对简单,但如果您需要帮助,我可以为您整理一些代码。请告诉我。
以下是我可能使用的代码:
static double DoCalc()
{
int uid;
if unit_roller is Angle
uid = 1;
else if unit_roller is Length
uid = 2;
//etc...
switch(uid)
case 1:
return AngleCalc(input);
case 2:
return LengthCalc(input);
//etc...
default;
}
static double AngleCalc(double input)
{
if (from_spinner == to_spinner)
return input;
else if (from_spinner is Degree && to_spinner is radian)
return input*0.0174532925
//etc...
}
我会说,一旦我看到它放在一起,这并不是真的更短,说它确实避免使用嵌套的if
语句,并且当你有大量的选项时可能会缩短你的代码