2015-04-12 63 views
-1

所以我想製作一個圓形計算器。一個人需要添加半徑,直徑,S或P,程序應該給出所有的答案,但是當我添加一件事情時,程序會使用我添加到程序中的所有公式。我希望程序使用那些特定的公式,例如,當我輸入radius(r)時,程序會使用這些公式(我們爲它插入r)而不是其他的公式。 我希望你們明白我的意思,抱歉我的英文不好,希望有人能幫忙。麻煩製作圓形計算器

public void onButtonClick(View v) { 
    EditText a1 = (EditText) findViewById(R.id.TFnum1); 
    EditText a2 = (EditText) findViewById(R.id.TFnum2); 
    EditText a3 = (EditText) findViewById(R.id.TFnum6); 
    EditText a4 = (EditText) findViewById(R.id.TFnum7); 


    TextView tv = (TextView) findViewById(R.id.TFnum7); //P 
    TextView tv1 = (TextView) findViewById(R.id.TFnum6); //S 
    TextView tv2 = (TextView) findViewById(R.id.TFnum2); //d 
    TextView tv3 = (TextView) findViewById(R.id.TFnum1); //r 
    boolean flag = false; 
    double num1, num2, num6, num7, ans; 
    num1 = ParseDouble(a1.getText().toString()); 
    num2 = ParseDouble(a2.getText().toString()); 
    num6 = ParseDouble(a3.getText().toString()); 
    num7 = ParseDouble(a4.getText().toString()); 
    ans = 0; 





    //when inserting r 
    //d 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
    else ans = 2 * num1; 
    tv2.setText(ans + ""); 

    //S 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
    else ans = 3.14 * (num1 * num1); 
    tv1.setText(ans + ""); 

    //P 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
    else ans = num1 * 3.14 * 2; 
    tv.setText(ans + ""); 



    //when inserting d 
    //r 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
    else ans = num2/2; 
    tv3.setText(ans + ""); 

    //S 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
    else ans = (num2/2) * (num2/2) * 3.14; 
    tv1.setText(ans + ""); 

    //P 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
    else ans = (num2/2) * 3.14 * 2; 
    tv.setText(ans + ""); 



    //when inserting S 
    //r 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
    else ans = Math.sqrt(num6/3.14); 
    tv3.setText(ans + ""); 

    //d 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
    else ans = Math.sqrt(num6/3.14) * 2; 
    tv2.setText(ans + ""); 

    //P 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
    else ans = (Math.sqrt(num6/3.14)) * 2 * 3.14; 
    tv.setText(ans + ""); 



    //when inserting P 
    //r 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
    else ans = num7/6.28; 
    tv3.setText(ans + ""); 

    //d 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
    else ans = (num7/6.28) * 2; 
    tv2.setText(ans + ""); 

    //S 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
    else ans = (num7/6.28) * (num7 /6.28) * 3.14; 
    tv1.setText(ans + ""); 
} 

private double ParseDouble(String number) { 
    if (number!= null && number.length() > 0) { 
     try { 
      return Double.parseDouble(number); 
     } catch(Exception e) { 
      return -1;// Will return -1 in case of exception, you can change it with another value 
     } 
    } 

    return 1; 
} 
+1

你的if(v.getId()== R.id.Badd)if(num2 == 0)flag = true;'總是一樣的!並且'v.getId()'返回什麼值'' – Prashant

+0

是的,看起來你複製/粘貼了'if'檢查,並忘記將它們專門化爲實際案例 - 順便說一句可以用一個''getId()''開關'也許有點清潔。 – ChiefTwoPencils

回答

0

問題是你有「onClick()」函數中的所有方法。所以它貫穿每一個。

這是我建議你做的。


您可以分別創建4種方法,分別針對每個參數(R,S,D,P)。然後在onClick()函數中,您將檢查輸入的內容,因此如果輸入了「R」,您將調用相應的方法併發送相應的視圖。

我希望下面的代碼解釋了它好一點:


聲明全局以下(即方法外):

boolean flag = false; 
TextView tv; 
TextView tv1; 
TextView tv2; 
TextView tv3; 

public void onButtonClick(View v) { 
    EditText a1 = (EditText) findViewById(R.id.TFnum1); 
    EditText a2 = (EditText) findViewById(R.id.TFnum2); 
    EditText a3 = (EditText) findViewById(R.id.TFnum6); 
    EditText a4 = (EditText) findViewById(R.id.TFnum6); 


    tv = (TextView) findViewById(R.id.TFnum7); //P 
    tv1 = (TextView) findViewById(R.id.TFnum6); //S 
    tv2 = (TextView) findViewById(R.id.TFnum2); //d 
    tv3 = (TextView) findViewById(R.id.TFnum1); //r 

    double num1, num2, num6, num7, ans; 
    num1 = ParseDouble(a1.getText().toString()); 
    num2 = ParseDouble(a2.getText().toString()); 
    num6 = ParseDouble(a3.getText().toString()); 
    num7 = ParseDouble(a4.getText().toString()); 
    ans = 0; 
    //this is where you will check whether values were entered 
    //and also depending on which EditText is accepting the R value 
    //for argument sake 
    if(!a1.equals("")){ 
     onInsertR(v, num2, num1, ans); 
    } 

if(!a2.equals ("")){onInsertR(v, num2,num1,ans);} 

if(!a3.equals ("")){onInsertR(v, num2,num1,ans);} 

if(!a4.equals ("")){onInsertR(v, num2,num1,ans);} 



} 

public void onInsertR(View v, double num2, double num1, double ans){ 
    //when inserting r 
    //d 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
     else ans = 2 * num1; 
    tv2.setText(ans + ""); 

    //S 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
     else ans = 3.14 * (num1 * num1); 
    tv1.setText(ans + ""); 

    //P 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
     else ans = num1 * 3.14 * 2; 
    tv.setText(ans + ""); 
} 

public void onInsertD(View v, double num2,double ans){ 
    //when inserting d 
    //r 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
     else ans = num2/2; 
    tv3.setText(ans + ""); 

    //S 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
     else ans = (num2/2) * (num2/2) * 3.14; 
    tv1.setText(ans + ""); 

    //P 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
     else ans = (num2/2) * 3.14 * 2; 
    tv.setText(ans + ""); 
} 

public void onInsertS(View v, double num2, double num6,double ans){ 
    //when inserting S 
    //r 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
     else ans = Math.sqrt(num6/3.14); 
    tv3.setText(ans + ""); 

    //d 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
     else ans = Math.sqrt(num6/3.14) * 2; 
    tv2.setText(ans + ""); 

    //P 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
     else ans = (Math.sqrt(num6/3.14)) * 2 * 3.14; 
    tv.setText(ans + ""); 
} 

public void onInsertP(View v, double num2, double num7,double ans){ 
    //when inserting P 
    //r 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
     else ans = num7/6.28; 
    tv3.setText(ans + ""); 

    //d 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
     else ans = (num7/6.28) * 2; 
    tv2.setText(ans + ""); 

    //S 
    if (v.getId() == R.id.Badd) if (num2 == 0) flag = true; 
     else ans = (num7/6.28) * (num7 /6.28) * 3.14; 
    tv1.setText(ans + ""); 
} 

private double ParseDouble(String number) { 
    if (number!= null && number.length() > 0) { 
     try { 
      return Double.parseDouble(number); 
     } catch(Exception e) { 
      return -1;// Will return -1 in case of exception, you can change  it with another value 
     } 
    } 

return 1; 
} 

希望這可以幫助你。

讓我知道它是怎麼回事:)

一切順利!

+0

我不能放棄在num2條件下消除或至少指出過度重複設置flag的機會。很明顯,只有在獲得num2後才需要執行一次。 – ChiefTwoPencils

+0

忘記提及'if(v.getId()== R.id.Badd)'的相似性......在相同的方法中檢查多次的意義何在?有沒有'V'改變所以... – ChiefTwoPencils

+0

@TejjD謝謝你的回答,我複製你的代碼,它告訴「無法解析符號」標誌「」和電視,tv1,tv2和tv3 –