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

下面我们有 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: ");


}

}


PIPIONE
浏览 236回答 3
3回答

HUH函数

为了编写您的代码,您必须清楚您正在实施的解决方案。一个很好的初学者练习,是写一个程序执行的流程图(键盘前的纸)。我做了一个供你将来参考:因此,考虑到该算法,我实现了一个可能的解决方案(使用硬编码数据)。主类:public class MainE {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String[] homeTeam = { "q", "w", "e", "r", "t", "y", "u", "i", "o", "p"};&nbsp; &nbsp; &nbsp; &nbsp; String[] awayTeam = {"p", "o", "i", "u", "y", "t", "r", "e", "w", "q"};&nbsp; &nbsp; &nbsp; &nbsp; int[] homeScore = {5,1,3,5,6,1,10,4,3,2};&nbsp; &nbsp; &nbsp; &nbsp; int[] awayScore = {4,3,2,1,3,5,42,1,3,2};&nbsp; &nbsp; &nbsp; &nbsp; int sumHome = 0;&nbsp; &nbsp; &nbsp; &nbsp; int sumAway = 0;&nbsp; &nbsp; &nbsp; &nbsp; int drawCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; int highestHomeScore = homeScore[0];&nbsp; &nbsp; &nbsp; &nbsp; int highestAwayScore = awayScore[0];&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; for (int index = 0; index < 10; index++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(homeTeam[index] + " [" + homeScore[index] + "]"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " | " + awayTeam[index] + " [" + awayScore[index] + "] ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sumHome += homeScore[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sumAway += awayScore[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (homeScore[index] > highestHomeScore) highestHomeScore = homeScore[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (awayScore[index] > highestAwayScore) highestAwayScore = awayScore[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(homeScore[index] == awayScore[index]) drawCount++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Totals");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("-------------------------------");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Total number of matches played: " + homeTeam.length);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Total of all home scores: " + sumHome);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Total of all away scores: " + sumAway);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Total number of draws: " + drawCount);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The highest home score: " + highestHomeScore);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The highest away score: " + highestAwayScore);&nbsp; &nbsp; }}输出:q [5] | p [4]&nbsp;w [1] | o [3]&nbsp;e [3] | i [2]&nbsp;r [5] | u [1]&nbsp;t [6] | y [3]&nbsp;y [1] | t [5]&nbsp;u [10] | r [42]&nbsp;i [4] | e [1]&nbsp;o [3] | w [3]&nbsp;p [2] | q [2]&nbsp;Totals-------------------------------Total number of matches played: 10Total of all home scores: 40Total of all away scores: 66Total number of draws: 2The highest home score: 10The highest away score: 42编辑:如果你想避免空值,你必须询问每次迭代if(homeTeam[index] != null ),也手动计算匹配(它们不再匹配数组长度)句柄空public class MainE {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String[] homeTeam = { "q", "w", "e", null, "t", "y", "u", "i", "o", "p"};&nbsp; &nbsp; &nbsp; &nbsp; String[] awayTeam = {"p", "o", "i", null, "y", "t", "r", "e", "w", "q"};&nbsp; &nbsp; &nbsp; &nbsp; int[] homeScore = {5,1,3,0,6,1,10,4,3,2};&nbsp; &nbsp; &nbsp; &nbsp; int[] awayScore = {4,3,2,0,3,5,42,1,3,2};&nbsp; &nbsp; &nbsp; &nbsp; int sumHome = 0;&nbsp; &nbsp; &nbsp; &nbsp; int sumAway = 0;&nbsp; &nbsp; &nbsp; &nbsp; int drawCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; int matches = 0;&nbsp; &nbsp; &nbsp; &nbsp; int highestHomeScore = homeScore[0];&nbsp; &nbsp; &nbsp; &nbsp; int highestAwayScore = awayScore[0];&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; for (int index = 0; index < 10; index++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(homeTeam[index] != null ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(homeTeam[index] + " [" + homeScore[index] + "]"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " | " + awayTeam[index] + " [" + awayScore[index] + "] ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sumHome += homeScore[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sumAway += awayScore[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (homeScore[index] > highestHomeScore) highestHomeScore = homeScore[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (awayScore[index] > highestAwayScore) highestAwayScore = awayScore[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(homeScore[index] == awayScore[index]) drawCount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matches++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Totals");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("-------------------------------");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Total number of matches played: " + matches);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Total of all home scores: " + sumHome);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Total of all away scores: " + sumAway);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Total number of draws: " + drawCount);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The highest home score: " + highestHomeScore);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The highest away score: " + highestAwayScore);&nbsp; &nbsp; }}输出:q [5] | p [4]&nbsp;w [1] | o [3]&nbsp;e [3] | i [2]&nbsp;t [6] | y [3]&nbsp;y [1] | t [5]&nbsp;u [10] | r [42]&nbsp;i [4] | e [1]&nbsp;o [3] | w [3]&nbsp;p [2] | q [2]&nbsp;Totals-------------------------------Total number of matches played: 9Total of all home scores: 35Total of all away scores: 65Total number of draws: 2The highest home score: 10The highest away score: 42注意:更好的选择是在询问输入时跳过空值。

喵喔喔

您的代码中有 2 个 for 循环,但您只需要 1 个即可获得所需的所有结果,因为所有数组的大小均为 10。因此在此循环中:for(index = 0; index < 10; index++) {&nbsp; &nbsp;// calculations}通过index在所有数组中使用,计算主队和客队的所有总和、平局数和最高分。还有一个建议:为变量使用更易读的名称,例如:sumMatches, sumHomeScore, sumAwayScore, sumDraws, sumHighestHome, sumHighestAway。

海绵宝宝撒

添加另一个名为drawCount.&nbsp;循环遍历 score 数组并检查循环索引处两个数组中的元素是否相等。如果是,则加drawCount一。然后在最后打印。此外,您可以将For循环合二为一。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java