我正在学习Java。我能够建立一个简单的篮球记分员应用程序,现在并试图弄清楚如何建立一个网球记分牌。
一旦A组比赛达到6分,我无法弄清楚如何更新第2组A组的值。一旦A队比赛达到6,它就会更新第1组
这是代码
Increase points on Team A */
public void incrementA(View view) {
pointsTeamA = pointsTeamA + 1;
{
if (pointsTeamA == 1) displayA(15);
if (pointsTeamA == 2) displayA(30);
if (pointsTeamA == 3) displayA(40);
if (pointsTeamA == 4) displayGamesA(gameTeamA = gameTeamA + 1);
if (pointsTeamA == 4) displayA(pointsTeamA = 0);
if (gameTeamA == 6) displaySet1A(gameTeamA);
if (gameTeamA == 6) displayGamesA(gameTeamA = 0);
if (gameTeamA == 6) displaySet2A(gameTeamA);
if (gameTeamA == 6) displayGamesA(gameTeamA = 0);
}
}
到目前为止,我的方法是:
/*This method displays points for Team A. */
private void displayA(int number) { TextView quantityTextView = (TextView) findViewById( R.id.points_team_A);
quantityTextView.setText("" + number); }
/*This method displays games for Team A */
private void displayGamesA(int number) { TextView quantityTextView = (TextView) findViewById( R.id.game_score_team_A);
quantityTextView.setText("" + number); }
/*This method displays Set 1 for Team A */
private void displaySet1A(int number) { TextView quantityTextView = (TextView) findViewById( R.id.set_1_team_A);
quantityTextView.setText("" + number); }
/*This method displays Set 2 for Team A */
private void displaySet2A(int number) { TextView quantityTextView = (TextView) findViewById( R.id.set_2_team_A);
quantityTextView.setText("" + number); }
}
任何帮助将不胜感激。
我将int
的所有值设置为零,例如int gameTeamA = 0;
项目的最后一部分: 目标:当两位玩家的得分均为40时,它会显示" Deuce"然后为了让玩家A获胜,它必须连续得2分,其中第1点将显示" ADV"并且第2点将结束游戏,为gameTeamA添加一个分数并开始新游戏。
到目前为止代码:
public void incrementA(View view) {
pointsTeamA++;
displayDeuceA(null);
if (pointsTeamA == 1) displayA(15);
if (pointsTeamA == 2) displayA(30);
if (pointsTeamA == 3) displayA(40);
if (pointsTeamA == 3 && pointsTeamB == 3)
displayDeuceA("Deuce");
if (pointsTeamA == 4) {
displayGamesA(gameTeamA = gameTeamA + 1);
displayA(pointsTeamA = 0);
displayB(pointsTeamB = 0);
displayDeuceA(null);
displayDeuceB(null);
}
答案 0 :(得分:2)
创建多行If语句可能有所帮助,而不是多次复制相同的语句。
public void incrementA(View view) {
pointsTeamA++;
if (pointsTeamA == 1) displayA(15);
if (pointsTeamA == 2) displayA(30);
if (pointsTeamA == 3) displayA(40);
if (pointsTeamA == 4) {
displayGamesA(gameTeamA = gameTeamA + 1);
displayA(pointsTeamA = 0);
}
if (gameTeamA == 6) {
displaySet1A(gameTeamA);
displayGamesA(gameTeamA = 0);
displaySet2A(gameTeamA);
displayGamesA(gameTeamA = 0);
}
}
话虽如此,在你向我们提供你所使用的方法之前,你所提到的问题并不完全清楚,所以我们可以看到它们应该做什么。据我所知,每当得分达到6,你就会更新Set 1和Set 2的值;我相信你应该有一个整数变量来跟踪游戏的设置,以及一个if语句内部增量,它根据这个变量更新一组的值。
编辑:以下是如何完成
首先,在类本身中创建一个静态字段。
public static int set = 1;
在任何方法之外,在你的课堂声明之后加上它。
public void incrementA(View view) {
pointsTeamA++;
if (pointsTeamA == 1) displayA(15);
if (pointsTeamA == 2) displayA(30);
if (pointsTeamA == 3) displayA(40);
if (pointsTeamA == 4) {
displayGamesA(gameTeamA = gameTeamA + 1);
displayA(pointsTeamA = 0);
}
if (gameTeamA == 6) {
if (set == 1) {
displaySet1A(gameTeamA);
displayGamesA(gameTeamA = 0);
set++; // Adds to the set variable, making it 2
} else if (set == 2) {
displaySet2A(gameTeamA);
displayGamesA(gameTeamA = 0);
}
}
}
编辑2:处理结果,优势,当分数超过3时获胜。替换先前的if-statement for pointsTeamA == 4
if (pointsTeamA >= 4) {
if (pointsTeamA - pointsTeamB >= 2) { // if team A wins
displayGamesA(gameTeamA = gameTeamA + 1);
displayA(pointsTeamA = 0);
displayB(pointsTeamB = 0);
displayDeuceA(null);
displayDeuceB(null);
} else if (pointsTeamA - pointsTeamB == 1) { // score up by one
// code for displaying "Advantage"
} else { // scores are tied
// code for displaying "Deuce"
}
}