2013-03-02 34 views
-3

我是新來的Java編程,所以請原諒我的愚蠢的問題,Android的心臟風險計算

一個例子是這樣的:假設你是一個30yo女性,你開始-9點。如果你的總膽固醇是180,那麼增加4分,如果你的吸菸者增加8分,如果你的HDL膽固醇超過60,減去1分,並且如果你的血壓是135,那麼增加1分。給你+3的結果。

我很難搞清楚如何將各個分數數字加在一起。

這是我到目前爲止已經試過:

 private void calculateForWomen(){ 

    // For Women 
    // 20–34 years: Minus 7 points. 35–39 years: Minus 3 points. 
    // 40–44 years: 0 points. 45–49 years: 3 points. 
    // 50–54 years: 6 points. 55–59 years: 8 points. 
    // 60–64 years: 10 points. 65–69 years: 12 points. 
    // 70–74 years: 14 points. 75–79 years: 16 points. 

    String strIsSmoker = spSmoker.getSelectedItem().toString(); 

    strAge = etAge.getText().toString(); 
    strTotalCholesterol = etTotalCholesterol.getText().toString(); 
    strHDL = etHDL.getText().toString(); 
    strSBP = etSBP.getText().toString(); 

    int age = Integer.valueOf(strAge); 
    int totalcholesterol = Integer.valueOf(strTotalCholesterol); 
    int hdl = Integer.valueOf(strHDL); 
    int sbp = Integer.valueOf(strSBP); 

    if(age >= 20 || age <= 34 ){ // Minus 7 points 

     // First point 
     points = points - 7; 

     // Second point 
     if(totalcholesterol <= 160){ // 0 point 
      points = points + 0; 

     } 
     else if(totalcholesterol >= 160 || totalcholesterol <= 199){ // 4 points 
      points = points + 4; 
     } 
     else if(totalcholesterol >= 200 || totalcholesterol <= 239){ // 8 points 
      points = points + 8; 
     } 
     else if(totalcholesterol >= 240 || totalcholesterol <= 279){ // 11 points 
      points += 11; 
     } 
     else if(totalcholesterol >=280){ // 13 points 
      points += 13; 
     } 

     // Third point 
     if(hdl >= 60) // Minus 1 point 
     { 
      points -= 1; 
     } 
     else if(hdl >= 50 || hdl <= 59){ // 1 point 
      points += 1; 
     } 
     else if(hdl <= 40){ // 2 points 
      points += 2; 
     } 

     // Fourth point 
     if(sbp == 120){ // 0 point 
      points += 0; 
     } 
     else if(sbp >= 120 || sbp <= 129){ // 1 point 
      points += 1; 
     } 
     else if(sbp >= 130 || sbp <= 139){ // 2 points 
      points += 2; 
     } 
     else if(sbp >= 140 || sbp <= 159){ // 3 points 
      points += 3; 
     } 
     else if(sbp >= 160){ // 4 points 
      points += 4; 
     } 

     // Fifth point 
     // 9 points 
     if(strIsSmoker.equals("Yes")){ 
      points += 9; 
     } // 0 point 
     else if(strIsSmoker.equals("No")){ 
      points += 0; 
     } 

     totalPoints = points; 
     String strTotalPoints = String.valueOf(totalPoints); 

     Toast.makeText(getBaseContext(), "Your score is " + strTotalPoints, Toast.LENGTH_LONG).show(); 

    } 
    else if(age >= 35 || age <= 39){ 

        // First point 
        points = points - 3; 

        // Second point 
        if(totalcholesterol <= 160){ // 0 point 
         points = points + 0; 
        } 
        else if(totalcholesterol >= 160 || totalcholesterol <= 199){ // 4 points 
         points = points + 4; 
        } 
        else if(totalcholesterol >= 200 || totalcholesterol <= 239){ // 8 points 
         points = points + 8; 
        } 
        else if(totalcholesterol >= 240 || totalcholesterol <= 279){ // 11 points 
         points += 11; 
        } 
        else if(totalcholesterol >=280){ // 13 points 
         points += 13; 
        } 

        // Third point 
        if(hdl >= 60) // Minus 1 point 
        { 
         points -= 1; 
        } 
        else if(hdl >= 50 || hdl <= 59){ // 1 point 
         points += 1; 
        } 
        else if(hdl <= 40){ // 2 points 
         points += 2; 
        } 

        // Fourth point 
        if(sbp == 120){ // 0 point 
         points += 0; 
        } 
        else if(sbp >= 120 || sbp <= 129){ // 1 point 
         points += 1; 
        } 
        else if(sbp >= 130 || sbp <= 139){ // 2 points 
         points += 2; 
        } 
        else if(sbp >= 140 || sbp <= 159){ // 3 points 
         points += 3; 
        } 
        else if(sbp >= 160){ // 4 points 
         points += 4; 
        } 

        // Fifth point 
        // 9 points 
        if(strIsSmoker.equals("Yes")){ 
         points += 9; 
        } // 0 point 
        else if(strIsSmoker.equals("No")){ 
         points += 0; 
        } 

        totalPoints = points; 
        String strTotalPoints = String.valueOf(totalPoints); 

        Toast.makeText(getBaseContext(), "Your score is " + strTotalPoints, Toast.LENGTH_LONG).show(); 

    } 
    else if(age >= 40 || age <= 44){ 

     // First point 
     points += 0; 

     // Second point 
     if(totalcholesterol <= 160){ // 0 point 
      points += 0; 
     } 
     else if(totalcholesterol >= 160 || totalcholesterol <= 199){ // 3 points 
      points += 3; 
     } 
     else if(totalcholesterol >= 200 || totalcholesterol <= 239){ // 6 points 
      points += 6; 
     } 
     else if(totalcholesterol >= 240 || totalcholesterol <= 279){ // 8 points 
      points += 8; 
     } 
     else if(totalcholesterol >=280){ // 10 points 
      points += 10; 
     } 

     // Third point 
     if(hdl >= 60) // Minus 1 point 
     { 
      points -= 1; 
     } 
     else if(hdl >= 50 || hdl <= 59){ // 1 point 
      points += 1; 
     } 
     else if(hdl <= 40){ // 2 points 
      points += 2; 
     } 

     // Fourth point 
     if(sbp == 120){ // 0 point 
      points += 0; 
     } 
     else if(sbp >= 120 || sbp <= 129){ // 1 point 
      points += 1; 
     } 
     else if(sbp >= 130 || sbp <= 139){ // 2 points 
      points += 2; 
     } 
     else if(sbp >= 140 || sbp <= 159){ // 3 points 
      points += 3; 
     } 
     else if(sbp >= 160){ // 4 points 
      points += 4; 
     } 
     // Fifth point 
     // 7 points 
     if(strIsSmoker.equals("Yes")){ 
      points += 7;        
     } 
     else if(strIsSmoker.equals("No")){ 
      points += 0;          
     } 

     totalPoints = points; 
     String strTotalPoints = String.valueOf(totalPoints); 

     Toast.makeText(getBaseContext(), "Your score is " + strTotalPoints, Toast.LENGTH_LONG).show(); 


    } 
    else if(age >= 45 || age <= 49){ 

     // First point 
     points += 3; 

     // Second point 
     if(totalcholesterol <= 160){ // 0 point 
      points += 0; 
     } 
     else if(totalcholesterol >= 160 || totalcholesterol <= 199){ // 3 points 
      points += 3; 
     } 
     else if(totalcholesterol >= 200 || totalcholesterol <= 239){ // 6 points 
      points += 6; 
     } 
     else if(totalcholesterol >= 240 || totalcholesterol <= 279){ // 8 points 
      points += 8; 
     } 
     else if(totalcholesterol >=280){ // 10 points 
      points += 10; 
     } 


     // Third point 
     if(hdl >= 60) // Minus 1 point 
     { 
      points -= 1; 
     } 
     else if(hdl >= 50 || hdl <= 59){ // 1 point 
      points += 1; 
     } 
     else if(hdl <= 40){ // 2 points 
      points += 2; 
     } 

     // Fourth point 
     if(sbp == 120){ // 0 point 
      points += 0; 
     } 
     else if(sbp >= 120 || sbp <= 129){ // 1 point 
      points += 1; 
     } 
     else if(sbp >= 130 || sbp <= 139){ // 2 points 
      points += 2; 
     } 
     else if(sbp >= 140 || sbp <= 159){ // 3 points 
      points += 3; 
     } 
     else if(sbp >= 160){ // 4 points 
      points += 4; 
     } 

     // Fifth point 
     // 7 points 
        if(strIsSmoker.equals("Yes")){ 
         points += 7;        
        } 
        else if(strIsSmoker.equals("No")){ 
         points += 0;        
        } 

        totalPoints = points; 
        String strTotalPoints = String.valueOf(totalPoints); 

        Toast.makeText(getBaseContext(), "Your score is " + strTotalPoints, Toast.LENGTH_LONG).show(); 


    } 
    else if(age >= 50 || age <= 54){ 
     // First point 
     points += 6; 

     // Second point 
     if(totalcholesterol <= 160){ // 0 point 
      points += 0; 
     } 
     else if(totalcholesterol >= 160 || totalcholesterol <= 199){ // 2 points 
      points += 2; 
     } 
     else if(totalcholesterol >= 200 || totalcholesterol <= 239){ // 4 points 
      points += 4; 
     } 
     else if(totalcholesterol >= 240 || totalcholesterol <= 279){ // 5 points 
      points += 5; 
     } 
     else if(totalcholesterol >=280){ // 7 points 
      points += 7; 
     } 

     // Third point 
     if(hdl >= 60) // Minus 1 point 
     { 
      points -= 1; 
     } 
     else if(hdl >= 50 || hdl <= 59){ // 1 point 
      points += 1; 
     } 
     else if(hdl <= 40){ // 2 points 
      points += 2; 
     } 

     // Fourth point 
     if(sbp == 120){ // 0 point 
      points += 0; 
     } 
     else if(sbp >= 120 || sbp <= 129){ // 1 point 
      points += 1; 
     } 
     else if(sbp >= 130 || sbp <= 139){ // 2 points 
      points += 2; 
     } 
     else if(sbp >= 140 || sbp <= 159){ // 3 points 
      points += 3; 
     } 
     else if(sbp >= 160){ // 4 points 
      points += 4; 
     } 

     // Fifth point 
        // 4 points 
        if(strIsSmoker.equals("Yes")){ 
         points += 4;          
        } 
        else if(strIsSmoker.equals("No")){ 
         points += 0;          
        } 

        totalPoints = points; 
        String strTotalPoints = String.valueOf(totalPoints); 

        Toast.makeText(getBaseContext(), "Your score is " + strTotalPoints, Toast.LENGTH_LONG).show(); 


    } 
    else if(age >= 55 | age <= 59){ 

     // First point 
     points += 8; 

     // Second point 
     if(totalcholesterol <= 160){ // 0 point 
      points += 0; 
     } 
     else if(totalcholesterol >= 160 || totalcholesterol <= 199){ // 2 points 
      points += 2; 
     } 
     else if(totalcholesterol >= 200 || totalcholesterol <= 239){ // 4 points 
      points += 4; 
     } 
     else if(totalcholesterol >= 240 || totalcholesterol <= 279){ // 5 points 
      points += 5; 
     } 
     else if(totalcholesterol >=280){ // 7 points 
      points += 7; 
     } 

     // Third point 
     if(hdl >= 60) // Minus 1 point 
     { 
      points -= 1; 
     } 
     else if(hdl >= 50 || hdl <= 59){ // 1 point 
      points += 1; 
     } 
     else if(hdl <= 40){ // 2 points 
      points += 2; 
     } 

     // Fourth point 
     if(sbp == 120){ // 0 point 
      points += 0; 
     } 
     else if(sbp >= 120 || sbp <= 129){ // 1 point 
      points += 1; 
     } 
     else if(sbp >= 130 || sbp <= 139){ // 2 points 
      points += 2; 
     } 
     else if(sbp >= 140 || sbp <= 159){ // 3 points 
      points += 3; 
     } 
     else if(sbp >= 160){ // 4 points 
      points += 4; 
     } 

     // Fifth point 
        // 4 points 
        if(strIsSmoker.equals("Yes")){ 
         points += 4;                
        } 
        else if(strIsSmoker.equals("No")){ 
         points += 0;              
        } 

     totalPoints = points; 
     String strTotalPoints = String.valueOf(totalPoints); 

     Toast.makeText(getBaseContext(), "Your score is " + strTotalPoints, Toast.LENGTH_LONG).show(); 


    } 
    else if(age >= 60 || age <= 64){ 

     // First point 
     points += 10; 

     // Second point 
     if(totalcholesterol <= 160){ // 0 point 
      points += 0; 
     } 
     else if(totalcholesterol >= 160 || totalcholesterol <= 199){ // 1 points 
      points += 1; 
     } 
     else if(totalcholesterol >= 200 || totalcholesterol <= 239){ // 2 points 
      points += 2; 
     } 
     else if(totalcholesterol >= 240 || totalcholesterol <= 279){ // 3 points 
      points += 3; 
     } 
     else if(totalcholesterol >=280){ // 4 points 
      points += 4; 
     } 

     // Third point 
     if(hdl >= 60) // Minus 1 point 
     { 
      points -= 1; 
     } 
     else if(hdl >= 50 || hdl <= 59){ // 1 point 
      points += 1; 
     } 
     else if(hdl <= 40){ // 2 points 
      points += 2; 
     } 

     // Fourth point 
     if(sbp == 120){ // 0 point 
      points += 0; 
     } 
     else if(sbp >= 120 || sbp <= 129){ // 1 point 
      points += 1; 
     } 
     else if(sbp >= 130 || sbp <= 139){ // 2 points 
      points += 2; 
     } 
     else if(sbp >= 140 || sbp <= 159){ // 3 points 
      points += 3; 
     } 
     else if(sbp >= 160){ // 4 points 
      points += 4; 
     } 

     // Fifth point 
        // 2 points 
        if(strIsSmoker.equals("Yes")){ 
         points += 2;                   
        } 
        else if(strIsSmoker.equals("No")){ 
         points += 0;                  
        } 

     totalPoints = points; 
     String strTotalPoints = String.valueOf(totalPoints); 

     Toast.makeText(getBaseContext(), "Your score is " + strTotalPoints, Toast.LENGTH_LONG).show(); 


    } 
    else if(age >= 65 || age <= 69){ 

     // First point 
     points += 12; 

     // Second point 
     if(totalcholesterol <= 160){ // 0 point 
      points += 0; 
     } 
     else if(totalcholesterol >= 160 || totalcholesterol <= 199){ // 1 points 
      points += 1; 
     } 
     else if(totalcholesterol >= 200 || totalcholesterol <= 239){ // 2 points 
      points += 2; 
     } 
     else if(totalcholesterol >= 240 || totalcholesterol <= 279){ // 3 points 
      points += 3; 
     } 
     else if(totalcholesterol >=280){ // 4 points 
      points += 4; 
     } 

     // Third point 
     if(hdl >= 60) // Minus 1 point 
     { 
      points -= 1; 
     } 
     else if(hdl >= 50 || hdl <= 59){ // 1 point 
      points += 1; 
     } 
     else if(hdl <= 40){ // 2 points 
      points += 2; 
     } 

     // Fourth point 
     if(sbp == 120){ // 0 point 
      points += 0; 
     } 
     else if(sbp >= 120 || sbp <= 129){ // 1 point 
      points += 1; 
     } 
     else if(sbp >= 130 || sbp <= 139){ // 2 points 
      points += 2; 
     } 
     else if(sbp >= 140 || sbp <= 159){ // 3 points 
      points += 3; 
     } 
     else if(sbp >= 160){ // 4 points 
      points += 4; 
     } 

     // Fifth point 
        // 2 points 
        if(strIsSmoker.equals("Yes")){ 
         points += 2;                  
        } 
        else if(strIsSmoker.equals("No")){ 
         points += 0;                  
        } 

     totalPoints = points; 
     String strTotalPoints = String.valueOf(totalPoints); 

     Toast.makeText(getBaseContext(), "Your score is " + strTotalPoints, Toast.LENGTH_LONG).show(); 


    } 
    else if(age >= 70 || age <= 74){ 

     // First point 
     points += 14; 

     // Second point 
     if(totalcholesterol <= 160){ // 0 point 
      points += 0; 
     } 
     else if(totalcholesterol >= 160 || totalcholesterol <= 199){ // 1 points 
      points += 1; 
     } 
     else if(totalcholesterol >= 200 || totalcholesterol <= 239){ // 1 points 
      points += 1; 
     } 
     else if(totalcholesterol >= 240 || totalcholesterol <= 279){ // 2 points 
      points += 2; 
     } 
     else if(totalcholesterol >=280){ // 2 points 
      points += 2; 
     } 

     // Third point 
     if(hdl >= 60) // Minus 1 point 
     { 
      points -= 1; 
     } 
     else if(hdl >= 50 || hdl <= 59){ // 1 point 
      points += 1; 
     } 
     else if(hdl <= 40){ // 2 points 
      points += 2; 
     } 

     // Fourth point 
     if(sbp == 120){ // 0 point 
      points += 0; 
     } 
     else if(sbp >= 120 || sbp <= 129){ // 1 point 
      points += 1; 
     } 
     else if(sbp >= 130 || sbp <= 139){ // 2 points 
      points += 2; 
     } 
     else if(sbp >= 140 || sbp <= 159){ // 3 points 
      points += 3; 
     } 
     else if(sbp >= 160){ // 4 points 
      points += 4; 
     } 

     // Fifth point 
        // 1 point 
        if(strIsSmoker.equals("Yes")){ 
         points += 1;                   
        } 
        else if(strIsSmoker.equals("No")){ 
         points += 0;                
        } 

     totalPoints = points; 
     String strTotalPoints = String.valueOf(totalPoints); 

     Toast.makeText(getBaseContext(), "Your score is " + strTotalPoints, Toast.LENGTH_LONG).show(); 


    } 
    else if(age >= 75 || age <= 79){ 

     // First point 
     points += 16; 

     // Second point 
     if(totalcholesterol <= 160){ // 0 point 
      points += 0; 
     } 
     else if(totalcholesterol >= 160 || totalcholesterol <= 199){ // 1 points 
      points += 1; 
     } 
     else if(totalcholesterol >= 200 || totalcholesterol <= 239){ // 1 points 
      points += 1; 
     } 
     else if(totalcholesterol >= 240 || totalcholesterol <= 279){ // 2 points 
      points += 2; 
     } 
     else if(totalcholesterol >=280){ // 2 points 
      points += 2; 
     } 

     // Third point 
     if(hdl >= 60) // Minus 1 point 
     { 
      points -= 1; 
     } 
     else if(hdl >= 50 || hdl <= 59){ // 1 point 
      points += 1; 
     } 
     else if(hdl <= 40){ // 2 points 
      points += 2; 
     } 

     // Fourth point 
     if(sbp == 120){ // 0 point 
      points += 0; 
     } 
     else if(sbp >= 120 || sbp <= 129){ // 1 point 
      points += 1; 
     } 
     else if(sbp >= 130 || sbp <= 139){ // 2 points 
      points += 2; 
     } 
     else if(sbp >= 140 || sbp <= 159){ // 3 points 
      points += 3; 
     } 
     else if(sbp >= 160){ // 4 points 
      points += 4; 
     } 

     // Fifth point 
        // 1 point 
        if(strIsSmoker.equals("Yes")){ 
         points += 1;                      
        } 
        else if(strIsSmoker.equals("No")){ 
         points += 0;                     
        } 

     totalPoints = points; 
     String strTotalPoints = String.valueOf(totalPoints); 

     Toast.makeText(getBaseContext(), "Your score is " + strTotalPoints, Toast.LENGTH_LONG).show(); 


    } 

} 

如果有人可以幫我優化我的代碼,我會很樂意欣賞它。提前致謝!

更新: 當我試圖從另一個年齡段選擇(40+),我一直得到的答案是不正確。

+1

而是通過英里運行的代碼,你爲什麼不告訴我們你的問題究竟在哪裏? – ElefantPhace 2013-03-02 17:55:27

+0

[SSCCE](http://sscce.org/)。 – 2013-03-02 17:56:19

+0

@ElefantPhace正如我所說的,我想優化我的代碼,如果你能幫助我。 – 2013-03-02 17:56:21

回答

2

如果要檢查一定範圍內的值,你應該使用&&||

if(age >= 20 || age <= 34 ) 

應該

if(age >= 20 && age <= 34 ) 

所以,你應該在所有的代碼更正此!

  • 這是沒用的,做points += 0;

  • if(strIsSmoker.equals("Yes"))更好地爲if("Yes".equalsIgnoreCase(strIsSmoker))

  • 避免在Toast.makeText()使用getBaseContext()只是使用thisACTIVITY_CLASS_NAME.this

+0

我是否需要改變我所有的||至 &&? – 2013-03-02 18:00:34

+0

是的,如果您在**某個範圍 – iTech 2013-03-02 18:01:20

+0

內檢查**值,那麼我應該刪除這個點+ = 0; – 2013-03-02 18:02:25