为了账号安全,请及时绑定邮箱和手机立即绑定

如何检查我有多少抽奖结果并显示它?

如何检查我有多少抽奖结果并显示它?

PIPIONE 2021-12-10 12:25:41
下面我们有 2 个 int 数组,用于存储主客场的足球比赛结果……我需要显示我有多少平局结果的统计数据。有人可以帮我看看吗?我想不通。请假设这是我的第一次曾经编程,如果你有任何解决方案,你可以评论代码吗..我需要向我的导师解释我是如何做到的。谢谢    String[] HomeTeam = new String[10];    String[] AwayTeam = new String[10];    int[] HomeScore = new int[10];    int[] AwayScore = new int[10];    int index = 0;    int sum = 0;    int sum1 = 0;    do     {        System.out.print("Enter Home Team Name: ");        HomeTeam[index] = kbd.nextLine();        System.out.print("Enter Away Team Name: ");        AwayTeam[index] = kbd.nextLine();        System.out.print("Enter Home Team Score:");        HomeScore[index] = kbd.nextInt();        System.out.print("Enter Away Team Score: ");        AwayScore[index] = kbd.nextInt();        kbd.nextLine();    } while(index < 10);    index = 0;    System.out.println();       do     {        System.out.println(HomeTeam[index] + " [" + HomeScore[index] + "]" + " | " + AwayTeam[index] + " [" + AwayScore[index] + "] ");        index = index + 1;    } while(index < 10);    kbd.close();    for(index = 0; index < 10; index++)        sum += HomeScore[index];        for(index = 0; index < 10; index++)            sum1 += AwayScore[index];    System.out.println();    System.out.println("Totals");    System.out.println("-------------------------------");    System.out.println("Total number of matches played: " + index);    System.out.println("Total of all home scores: " + sum);    System.out.println("Total of all away scores: " + sum1);    System.out.println("Total number of draws: ");    System.out.println("The highest home score: ");    System.out.println("The highest away score: ");}}
查看完整描述

3 回答

?
HUH函数

TA贡献1836条经验 获得超4个赞

为了编写您的代码,您必须清楚您正在实施的解决方案。一个很好的初学者练习,是写一个程序执行的流程图(键盘前的纸)。

我做了一个供你将来参考:

//img1.sycdn.imooc.com//61b2d6df0001ca8005280952.jpg

因此,考虑到该算法,我实现了一个可能的解决方案(使用硬编码数据)。


主类:

public class MainE {


    public static void main(String[] args) {


        String[] homeTeam = { "q", "w", "e", "r", "t", "y", "u", "i", "o", "p"};

        String[] awayTeam = {"p", "o", "i", "u", "y", "t", "r", "e", "w", "q"};

        int[] homeScore = {5,1,3,5,6,1,10,4,3,2};

        int[] awayScore = {4,3,2,1,3,5,42,1,3,2};


        int sumHome = 0;

        int sumAway = 0;

        int drawCount = 0;


        int highestHomeScore = homeScore[0];

        int highestAwayScore = awayScore[0];


        System.out.println();


        for (int index = 0; index < 10; index++) {


            System.out.println(homeTeam[index] + " [" + homeScore[index] + "]"

                    + " | " + awayTeam[index] + " [" + awayScore[index] + "] ");

            sumHome += homeScore[index];

            sumAway += awayScore[index];


            if (homeScore[index] > highestHomeScore) highestHomeScore = homeScore[index];

            if (awayScore[index] > highestAwayScore) highestAwayScore = awayScore[index];


            if(homeScore[index] == awayScore[index]) drawCount++;

        }




        System.out.println();

        System.out.println("Totals");

        System.out.println("-------------------------------");

        System.out.println("Total number of matches played: " + homeTeam.length);

        System.out.println("Total of all home scores: " + sumHome);

        System.out.println("Total of all away scores: " + sumAway);

        System.out.println("Total number of draws: " + drawCount);

        System.out.println("The highest home score: " + highestHomeScore);

        System.out.println("The highest away score: " + highestAwayScore);


    }


}

输出:

q [5] | p [4] 

w [1] | o [3] 

e [3] | i [2] 

r [5] | u [1] 

t [6] | y [3] 

y [1] | t [5] 

u [10] | r [42] 

i [4] | e [1] 

o [3] | w [3] 

p [2] | q [2] 


Totals

-------------------------------

Total number of matches played: 10

Total of all home scores: 40

Total of all away scores: 66

Total number of draws: 2

The highest home score: 10

The highest away score: 42

编辑:


如果你想避免空值,你必须询问每次迭代if(homeTeam[index] != null ),也手动计算匹配(它们不再匹配数组长度)


句柄空

public class MainE {


    public static void main(String[] args) {


        String[] homeTeam = { "q", "w", "e", null, "t", "y", "u", "i", "o", "p"};

        String[] awayTeam = {"p", "o", "i", null, "y", "t", "r", "e", "w", "q"};

        int[] homeScore = {5,1,3,0,6,1,10,4,3,2};

        int[] awayScore = {4,3,2,0,3,5,42,1,3,2};


        int sumHome = 0;

        int sumAway = 0;

        int drawCount = 0;

        int matches = 0;


        int highestHomeScore = homeScore[0];

        int highestAwayScore = awayScore[0];


        System.out.println();


        for (int index = 0; index < 10; index++) {

            if(homeTeam[index] != null ){

                System.out.println(homeTeam[index] + " [" + homeScore[index] + "]"

                        + " | " + awayTeam[index] + " [" + awayScore[index] + "] ");

                sumHome += homeScore[index];

                sumAway += awayScore[index];


                if (homeScore[index] > highestHomeScore) highestHomeScore = homeScore[index];

                if (awayScore[index] > highestAwayScore) highestAwayScore = awayScore[index];


                if(homeScore[index] == awayScore[index]) drawCount++;

                matches++;

            }



        }




        System.out.println();

        System.out.println("Totals");

        System.out.println("-------------------------------");

        System.out.println("Total number of matches played: " + matches);

        System.out.println("Total of all home scores: " + sumHome);

        System.out.println("Total of all away scores: " + sumAway);

        System.out.println("Total number of draws: " + drawCount);

        System.out.println("The highest home score: " + highestHomeScore);

        System.out.println("The highest away score: " + highestAwayScore);


    }


}

输出:

q [5] | p [4] 

w [1] | o [3] 

e [3] | i [2] 

t [6] | y [3] 

y [1] | t [5] 

u [10] | r [42] 

i [4] | e [1] 

o [3] | w [3] 

p [2] | q [2] 


Totals

-------------------------------

Total number of matches played: 9

Total of all home scores: 35

Total of all away scores: 65

Total number of draws: 2

The highest home score: 10

The highest away score: 42

注意:更好的选择是在询问输入时跳过空值。


查看完整回答
反对 回复 2021-12-10
?
喵喔喔

TA贡献1735条经验 获得超5个赞

您的代码中有 2 个 for 循环,但您只需要 1 个即可获得所需的所有结果,因为所有数组的大小均为 10。

因此在此循环中:


for(index = 0; index < 10; index++) {

   // calculations

}

通过index在所有数组中使用,计算主队和客队的所有总和、平局数和最高分。

还有一个建议:为变量使用更易读的名称,例如:

sumMatches, sumHomeScore, sumAwayScore, sumDraws, sumHighestHome, sumHighestAway。


查看完整回答
反对 回复 2021-12-10
?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

添加另一个名为drawCount. 循环遍历 score 数组并检查循环索引处两个数组中的元素是否相等。如果是,则加drawCount一。然后在最后打印。

此外,您可以将For循环合二为一。


查看完整回答
反对 回复 2021-12-10
  • 3 回答
  • 0 关注
  • 235 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信