Java中的计数器增量错误

时间:2016-05-17 14:26:43

标签: java android

我正在开发一款能够保持网球比分的应用。一切正常,除了我的下面代码中的几个问题。

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");

                    }
                }
            }

1 个答案:

答案 0 :(得分: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--;
}