排球分数;循环的逻辑

时间:2016-01-17 19:06:04

标签: android

我想知道为什么我的程序不能正常工作?它应该是一个在排球比赛中计算点数的应用程序。我使用的是Android Studio 1.5.1。 br1和br2是集合的计数器。 counter1和counter2是集合中各点的计数器。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button btn1;
    Button btn2;
    Button btn3;
    Button btn4;
    Button btn5;
    TextView text1;
    TextView text2;
    TextView text3;
    TextView text4;

    int counter1 = 0;
    int counter2 = 0;

    int br1;
    int br2;

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

        btn1 = (Button) findViewById(R.id.button);
        btn2 = (Button) findViewById(R.id.button2);
        btn3 = (Button) findViewById(R.id.button3);
        btn4 = (Button) findViewById(R.id.button4);
        btn5 = (Button) findViewById(R.id.button5);

        text1 = (TextView) findViewById(R.id.textView);
        text2 = (TextView) findViewById(R.id.textView2);
        text3 = (TextView) findViewById(R.id.textView6);
        text4 = (TextView) findViewById(R.id.textView7);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        for(br1=0, br2=0; br1<=3 || br2<=3; )
        {

             do { 
                if (v == btn1) {
                    counter1++;
                    text1.setText(Integer.toString(counter1));
                    text1.setBackgroundColor(Color.CYAN);
                }
                if (v == btn2) {
                    counter1--;
                    text1.setText(Integer.toString(counter1));
                    text1.setBackgroundColor(Color.GREEN);
                }

                if (v == btn3) {
                    counter2++;
                    text2.setText(Integer.toString(counter2));
                    text2.setBackgroundColor(Color.CYAN);
                }
                if (v == btn4) {
                    counter2--;
                    text2.setText(Integer.toString(counter2));
                    text2.setBackgroundColor(Color.GREEN);
                }

                if (v == btn5) {
                    counter1 = 0;
                    counter2 = 0;
                    text1.setText(Integer.toString(counter1));
                    text2.setText(Integer.toString(counter2));
                    text1.setBackgroundColor(Color.RED);
                    text2.setBackgroundColor(Color.RED);
                }

            } while (((counter1 - counter2 >= 2) || (counter2 - counter1 >= 2)) && (counter1 >= 25 || counter2 >= 25)); 

            if (counter1 > counter2) {
                br1++;
                text3.setText(Integer.toString(br1));
            } else {
                br2++;
                text4.setText(Integer.toString(br2));
            }

            counter1 = 0;
            counter2 = 0;
        }
    }
}

如果有人可以帮助我,我将非常感激。

1 个答案:

答案 0 :(得分:0)

我认为循环并不是你需要的东西。 我建议你可以试试像

这样的东西
@Overwrite
public void onClick(View v) {
    if(v == btn1) {
        counter1++;
        text1.setText(Integer.toString(counter1));
        text1.setBackgroundColor(Color.CYAN);
    }
    else if (v == btn2) {
        counter1--;
        text1.setText(Integer.toString(counter1));
        text1.setBackgroundColor(Color.GREEN);
    }
    else if (v == btn3) {
        counter2++;
        text2.setText(Integer.toString(counter2));
        text2.setBackgroundColor(Color.CYAN);
    }
    else if (v == btn4) {
        counter2--;
        text2.setText(Integer.toString(counter2));
        text2.setBackgroundColor(Color.GREEN);
    }
    else if (v == btn5) {
        counter1 = 0;
        counter2 = 0;
        text1.setText(Integer.toString(counter1));
        text2.setText(Integer.toString(counter2));
        text1.setBackgroundColor(Color.RED);
        text2.setBackgroundColor(Color.RED);
    }

    if(counter1 >= 25 && counter1 - counter2 >= 2) {
        br1++;
        text3.setText(Integer.toString(br1));
        counter1 = 0;
        counter2 = 0;
    }
    else if(counter2 >= 25 && counter2 - counter1 >=2) {
        br2++;
        text3.setText(Integer.toString(br2));
        counter1 = 0;
        counter2 = 0;
    }

    if(br1 - br2 > 2 || br2 - br1 > 2) {
        // handle end of the match
    }
}