当任何团队达到30分时打印吐司

问题描述

我创建了篮球计分卡应用程序。我想添加一个功能,即哪个球队先获得30分就赢得比赛(A队获胜的吐司消息),如果我们单击得分按钮,则不应增加得分。 / p>

感谢您的帮助。 我的代码

public class MainActivity extends AppCompatActivity {

    int scoreTeamA = 0;
    int scoreTeamB = 0;
    int foul_b = 0;
    int foul_a = 0;

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

        if(scoreTeamA>30||scoreTeamA==30)
        {
            Toast.makeText(MainActivity.this,"team A wins",Toast.LENGTH_SHORT).show();

        }
    }


    public void displayForTeamA(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_a_score);
        scoreView.setText(String.valueOf(score));
    }


    public void displayForTeamB(int score) {
        TextView scoreView = (TextView) findViewById(R.id.team_b_score);
        scoreView.setText(String.valueOf(score));
    }

    public void displayFiveForTeamA (View v){
        scoreTeamA = scoreTeamA + 5;
        displayForTeamA(scoreTeamA);
         
    }


    public void displayThreeForTeamA(View v) {
        scoreTeamA = scoreTeamA + 3;
        displayForTeamA(scoreTeamA);
      
    }

    public void displayFoulForTeamA(View v) {
        foul_a = foul_a + 1;
        String sA = "Total Foul : " + foul_a;
        scoreTeamA = scoreTeamA - 1;
        displayForTeamA(scoreTeamA);
        foulA(sA);
    }

    public void foulA(String f) {
        TextView scoreView = (TextView) findViewById(R.id.foul_a);
        scoreView.setText(String.valueOf(f));
    }


    public void displayFiveForTeamB(View v) {
        scoreTeamB = scoreTeamB + 5;
        displayForTeamB(scoreTeamB);
    }

    public void displayThreeForTeamB(View v) {
        scoreTeamB = scoreTeamB + 3;
        displayForTeamB(scoreTeamB);
    }

    public void displayFoulForTeamB(View v) {
        foul_b = foul_b + 1;
        String sB = "Total Foul : " + foul_b;
        scoreTeamB = scoreTeamB - 1;
        displayForTeamB(scoreTeamB);
        foulB(sB);
    }

    public void foulB(String foul) {
        TextView scoreView = (TextView) findViewById(R.id.foul_b);
        scoreView.setText(String.valueOf(foul));
    }

    public void resetscore(View v) {
        scoreTeamB = 0;
        scoreTeamA = 0;
        foul_b = 0;
        foul_a = 0;
        String s = "Total Foul : " + 0;
        displayForTeamA(scoreTeamA);
        displayForTeamB(scoreTeamB);
        foulA(s);
        foulB(s);
    }
}

解决方法

更改此

public void displayForTeamA(int score) {
            TextView scoreView = (TextView) findViewById(R.id.team_a_score);
            scoreView.setText(String.valueOf(score));
        }

    public void displayFiveForTeamA (View v){
            if(scoreTeamA < 30){
                scoreTeamA = scoreTeamA + 5;
                displayForTeamA(scoreTeamA);
            }
        }

将此条件添加到其他分数增加方法中,您所缺少的是每次调用该方法时都会增加分数,在添加分数之前需要检查分数是否已经等于或大于30比它。