微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java程序跟踪团队得分

如何解决Java程序跟踪团队得分

编辑:我从以前更新了以下代码,并且能够获取单个季度的金额。我不确定为什么我的“ teamTotal”方法没有将所有四个季度加在一起。我认为应该对团队1和2的所有分数进行迭代,并将它们加在一起以获得单独的总数。但是,当我运行它时,我得到的只是零。

public class Offical4 {

static int team1[] = new int[4];
static int team2[] = new int[4];
static int teamOnescore = 0;
static int teamTwoscore = 0;
static Scanner keyboard = new Scanner(system.in);

public static void main(String[] args) {


    for (int Quarter = 1; Quarter <= 4; Quarter++) {
        System.out.println("Quarter " + Quarter);
        for (int qtr = 0; qtr < 4; qtr++) {
            quarterScoring(team1,team2,qtr);

        }
        System.out.println("Q"+ (Quarter)  + " score for team 1 is " + teamOnescore);
        System.out.println("Q"+ (Quarter)  + " score for team 2 is " + teamTwoscore);
        System.out.println("\n");
    }
    int team1Total = teamTotal(team1);
    int team2Total = teamTotal(team2);

    displayGameResults(team1,team2);

    System.out.println("Team one total is " + team1Total);



}
static int pointsscored;

static int teamTotal(int[] team) {
    int sum = 0;

    for(int i =0; i < team.length; i++)
        sum += team[i];

    return sum;
}

static void quarterScoring(int[] team1,int[] team2,int qtr) {
        System.out.println("What team scored?(1 or 2)");
        int scoreResult = keyboard.nextInt();

        System.out.println("How many points did they score (1,2,or 3)");
        int pointsscored = keyboard.nextInt();

        if (scoreResult == 1) {
            teamOnescore += pointsscored;
        } else if (scoreResult == 2) {
            teamTwoscore += pointsscored;
        }
        if (scoreResult > 2) {
            System.out.println("Invalid team - quarter has ended.");
        }


}

static void displayGameResults(int[] team1,int[] team2) {
    System.out.println("Team 1 score is " + teamOnescore);
    System.out.println("Team 2 score is " + teamTwoscore);

    if(teamOnescore > teamTwoscore){
        System.out.println("Team one is the winner");
    }
    else{
        System.out.println("Team two Two wins");
    }
}

}

解决方法

使用代码顶部的两个数组来跟踪每个季度中每个团队的得分。然后,要获得它们的总数,请将数组的值相加。您可以为此使用增强的for循环。

,

有很多方法可以跟踪每个团队的总积分,下面是一个想法

public class Main {

    static int team1[] = new int[4];        // define here
    static int team2[] = new int[4];        // define here

    static int teamOneScore = 0;            // define here
    static int teamTwoScore = 0;            // define here

    static Scanner keyboard = new Scanner(System.in);

...

那么你可以

System.out.println("What team scored?(1 or 2)");
int iTeam = keyboard.nextInt();

System.out.println("How many points did they score (1,2,or 3)");
int pointsScored = keyboard.nextInt();
        
if (iTeam == 1) {
    teamOneScore += pointsScored;   // Total score of Team 1
}
else if (iTeam == 2) {
    teamTwoScore += pointsScored;   // Total score of Team 2
}

System.out.println("Team 1 score is " + teamOneScore);
System.out.println("Team 2 score is " + teamTwoScore);

还有一件事,我认为您需要先验证输入值是什么,然后再执行操作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。