2013-08-02 75 views
1

這是我的android應用程序單位轉換器。我有三個紡紗機:單元,。例如,角度,度& Radian。需要單位轉換器的程序邏輯

我爲單元微調器添加了一個監聽器。當選擇一個單位時,將被填充。用戶將從 EditText輸入的輸入,按下計算按鈕後,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 

這對於多個單元來說變得非常長。那麼你能提出另一個邏輯嗎?

回答

1

根據您的變量,您可以使用switch聲明。

switch(someintegervariable){ 
    case SOME_INT_CONSTANT_1: 
    /* ... */ 
    break; 
    case SOME_INT_CONSTANT_2: 
    /* ... */ 
    break; 
    default: 
    /* ... */ 
} 
+0

可以請您詳細說明嗎?有三個變量。打開什麼? –

+0

看到這個例子,你可以只打開整型常量(僅限Strings,Java 7+,在android上不可用)。 – dst

+0

在你的特殊情況下,你可以首先在值類型上做一個switch語句,然後在每個case block內部依次執行兩個例子:首先將to_value賦給你選擇的任何單位(常量),然後將結果帶到目標值。這是imho最短的方式;如果你需要高精度的話,你將不得不做3個嵌套switch語句。 – dst

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報表,並能當您有大量選項時可能會縮短您的代碼。

+0

是的一些代碼plz。 –

1

現在你有這種方式,你有兩個寫NxN if語句爲每個類別有N個單位。

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