打印当任何团队达到30分时的提示。

huangapple go评论152阅读模式
英文:

print the toast when any team reaches 30 points

问题

我创建了篮球记分卡应用程序。我想要添加一个功能,即首先达到30分的队伍获胜比赛(弹出消息提示队伍A获胜),如果我们点击得分按钮,则不应增加分数。

感谢您的帮助。
我的代码:

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) {
            Toast.makeText(MainActivity.this, "队伍A获胜", 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) {
        if (scoreTeamA < 30) {
            scoreTeamA = scoreTeamA + 5;
            displayForTeamA(scoreTeamA);
        }
    }

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

    public void displayFoulForTeamA(View v) {
        if (scoreTeamA > 0) {
            foul_a = foul_a + 1;
            String sA = "犯规总数: " + 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) {
        if (scoreTeamB < 30) {
            scoreTeamB = scoreTeamB + 5;
            displayForTeamB(scoreTeamB);
        }
    }

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

    public void displayFoulForTeamB(View v) {
        if (scoreTeamB > 0) {
            foul_b = foul_b + 1;
            String sB = "犯规总数: " + 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 = "犯规总数: " + 0;
        displayForTeamA(scoreTeamA);
        displayForTeamB(scoreTeamB);
        foulA(s);
        foulB(s);
    }
}
英文:

i create the basketball scorecard app.i want to add a functionality i.e which team reaches the 30 points first wins the game(toast message that team A wins) and if we click on point buttons it should not increase the points.

Thank you for the help.
My Code

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&gt;30||scoreTeamA==30)
{
Toast.makeText(MainActivity.this,&quot;team A wins&quot;,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 = &quot;Total Foul : &quot; + 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 = &quot;Total Foul : &quot; + 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 = &quot;Total Foul : &quot; + 0;
displayForTeamA(scoreTeamA);
displayForTeamB(scoreTeamB);
foulA(s);
foulB(s);
}
}

答案1

得分: 0

更改为:

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

将此条件添加到其他增加得分的方法中。你遗漏的是,在每次调用方法之前,你都在增加分数,你需要在添加分数之前检查是否已经等于30或大于30。

英文:

Change this

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

to

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

Add this if condition to your other score increasing methods, What you're missing is that you're increasing the score every time you call the method, before you add the score you need check if its already equal to 30 or greater than it.

huangapple
  • 本文由 发表于 2020年8月27日 02:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63603425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定