gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:39:45 25 4
gpt4 key购买 nike

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

}

}

最佳答案

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

我做了一个供您将来引用:

enter image description here

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

主类:

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
<小时/>

编辑:

如果你想避免 null 值,你必须在每次迭代时询问 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

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

关于java - 如何查看我有多少抽奖结果并显示它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53011600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com