2016-05-17 54 views
0

我正在開發一個應用程序來保持網球得分。一切工作正常,除了我的下面的代碼中的幾個問題。Java中的計數器增量錯誤

1)我必須在每次啓動應用程序時按下RESET按鈕,因爲櫃檯不斷增加並且它在條件下工作。 2)無法關閉set1到7-5。 set1繼續從7-5增加到8-5,9-5等

任何幫助將不勝感激。

public class MainActivity extends AppCompatActivity { 

    public int CountA; 
    public int CountB; 
    public int set1 = 0; 
    public int set2 = 0; 
    public int set3 = 0; 
    public int set4 = 0; 
    public int set5 = 0; 
    public int set6 = 0; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     CountA = 0; 
     CountB = 0; 

     final TextView textView1 = (TextView) findViewById(R.id.textView1); 
     final TextView textView2 = (TextView) findViewById(R.id.textView2); 
     final TextView textView3 = (TextView) findViewById(R.id.textView3); 

     final TextView textViewCount1 = (TextView) findViewById(R.id.textViewCount1); 
     final Button countBtn1 = (Button) findViewById(R.id.countBtn1); 


     final TextView textView4 = (TextView) findViewById(R.id.textView4); 
     final TextView textView5 = (TextView) findViewById(R.id.textView5); 
     final TextView textView6 = (TextView) findViewById(R.id.textView6); 

     final TextView textViewCount2 = (TextView) findViewById(R.id.textViewCount2); 
     final Button countBtn2 = (Button) findViewById(R.id.countBtn2); 

     final TextView textViewScore = (TextView) findViewById(R.id.textViewScore); 

     final Button resetBtn = (Button) findViewById(R.id.resetBtn); 



      final AlertDialog dialog = null; 
      final AlertDialog.Builder builder = new AlertDialog.Builder(this); 

     set1 = Integer.parseInt(textView1.getText().toString()); 
     set2 = Integer.parseInt(textView2.getText().toString()); 
     set3 = Integer.parseInt(textView3.getText().toString()); 
     set4 = Integer.parseInt(textView4.getText().toString()); 
     set5 = Integer.parseInt(textView5.getText().toString()); 
     set6 = Integer.parseInt(textView6.getText().toString()); 




     assert resetBtn !=null; 
     resetBtn.setOnClickListener(new View.OnClickListener() { 
             @Override 
             public void onClick(View v){ 
              CountA = 0; 
              CountB = 0; 
              textView1.setText("0"); 
              textView2.setText("0"); 
              textView3.setText("0"); 
              textView4.setText("0"); 
              textView5.setText("0"); 
              textView6.setText("0"); 
              textViewCount1.setText("0"); 
              textViewCount2.setText("0"); 
              textViewScore.setText("0"); 
              set1 = set2 = set3 = set4 = set5 = set6 = 0; 
             } 

            } 
     ); 


     //Methods for the Player 1 scores// 
     assert countBtn1 != null; 
     countBtn1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       CountA++; 
       //assert textViewScore != null; 
       textViewScore.setText(CountA+"--"+textViewCount1.getText()+"--"+textViewCount2.getText()); 
       if (CountA == 1) { 
        textViewCount1.setText("15"); 
       } else if (CountA == 2) { 
        textViewCount1.setText("30"); 
       } else if (CountA == 3) { 
        textViewCount1.setText("40"); 
       } else if (CountA == 4) { 
        if (CountB == 3 && textViewCount2.getText() == "40" && textViewCount1.getText() == "40") { 
         textViewCount1.setText("AD"); 
         CountA--; 
        } 
        if (CountB == 4 && textViewCount2.getText() == "40" && textViewCount1.getText() == "40") { 
         textViewCount1.setText("AD"); 
         CountA--; 
        } 
        if (CountA == 4 && textViewCount1.getText() == "40" && textViewCount2.getText() == "15") { 
         textViewCount1.setText("0"); 
         textViewCount2.setText("0"); 
         CountA--; 
         if(set1 != 7) { 
          set1 = set1 + 1; 
          textView1.setText("" + set1); 
          CountA=0; 
         } 
        } 
        if (CountA == 4 && textViewCount1.getText() == "40" && textViewCount2.getText() == "30") { 
         textViewCount1.setText("0"); 
         textViewCount2.setText("0"); 
         CountA--; 
         if(set1 != 7) { 
          set1 = set1 + 1; 
          textView1.setText("" + set1); 
          CountA=0; 
         } 
        } 
        else if (CountA == 4 && textViewCount1.getText() == "AD" && textViewCount2.getText() == "40") { 
         textViewCount1.setText("0"); 
         textViewCount2.setText("0"); 
         CountA--; 
         if(set1 != 7) { 
          set1 = set1 + 1; 
          textView1.setText("" + set1); 
          CountA=0; 
          CountB=0; 
         } 
        } 
        else if (CountA == 4 && textViewCount1.getText() == "40" && textViewCount2.getText() == "AD") { 
         textViewCount1.setText("40"); 
         textViewCount2.setText("40"); 
         CountA--; 
        } 
        else if (CountA == 4 && textViewCount1.getText() == "40" && textViewCount2.getText() == "0") { 
         textViewCount1.setText("0"); 
         textViewCount2.setText("0"); 
         CountA--; 
         if((set1 != 6 && set4 != 6 && set2 == 0 && set5 == 0) 
           || (set1 == 5 && set4 !=7 && set2 == 0 && set5 == 0) 
           || (set1 != 7 && set4 == 5 && set2 == 0 && set5 == 0) 
           || (set1 != 7 && set4 == 6 && set2 == 0 && set5 == 0)) 
         { 
          set1 = set1 + 1; 
          //textViewScore.setText(set1+"-"+set2+"-"+set3+"-"+set4+"-"+set5+"-"+set6); 
          textView1.setText("" + set1); 
          CountA=0; 
          if((set1 ==6 && set4 ==1 && set2 == 0 && set5 == 0) 
            ||(set1 ==6 && set4 ==2 && set2 == 0 && set5 == 0) 
            ||(set1 ==6 && set4 ==3 && set2 == 0 && set5 == 0) 
            ||(set1 ==6 && set4 ==4 && set2 == 0 && set5 == 0)) 

          { 
           builder.setMessage("Player 1 wins the 1st set"); 
           AlertDialog theAlertDialog = builder.create(); 
           theAlertDialog.show(); 
          } 
         } 
         else if((set2 <= 5 && set5 != 6 && set3 == 0 && set6 == 0) 
           || (set2 == 5 && set5 != 7 && set3 == 0 && set6 == 0) 
           || (set2 == 6 && set5 == 5 && set3 == 0 && set6 == 0) 
           || (set2 == 6 && set5 == 6 && set3 == 0 && set6 == 0)) 
         { 
          set2 = set2 + 1; 
          //textViewScore.setText("02"); 
          textView2.setText("" + set2); 
          CountA=0; 
         } 
         else if((set6 <= 4 && set3 != 7) 
           || (set6 == 5 && set3 != 7) 
           || (set6 == 6 && set3 != 7) 
           || (set6 == 7 && set3 != 7) 
           ) { 
          set3 = set3 + 1; 
          //textViewScore.setText("02"); 
          textView3.setText("" + set3); 
          CountA=0; 
         } 
         else{ 
          textViewScore.setText(set1+"-"+set2+"-"+set3+"-"+set4+"-"+set5+"-"+set6); 
          //textViewScore.setText("03"+CountB); 
          CountA = 0; 
         } 
    } 
        else if (CountA == 5 && textViewCount1.getText() == "40" && textViewCount2.getText() == "40") { 
         textViewCount1.setText("AD"); 
         textViewCount2.setText("40"); 

        } 
       } 
      } 

回答

0

要獲得TextView的文字作爲String使用textViewCount1.getText().toString()和比較String的使用equals(...)代替==

下面是一個例子:

if (CountB == 3 && textViewCount2.getText().toString().equals("40") && textViewCount1.getText().toString().equals("40")) { 
    textViewCount1.setText("AD"); 
    CountA--; 
} 
+0

大哥,你是天才。你解決了我的問題重置。 –