Java 如何检查我有多少绘图结果并显示?
下面我们有2个整数数组存储主客场的足球结果…我需要显示统计我有多少平局结果。有人能帮我一下吗?我想不出来。请假设这是我的第一个程序,如果你有任何解决方案,请你评论代码。我需要向我的导师解释我是如何做到的.谢谢Java 如何检查我有多少绘图结果并显示?,java,arrays,Java,Arrays,下面我们有2个整数数组存储主客场的足球结果…我需要显示统计我有多少平局结果。有人能帮我一下吗?我想不出来。请假设这是我的第一个程序,如果你有任何解决方案,请你评论代码。我需要向我的导师解释我是如何做到的.谢谢 String[] HomeTeam = new String[10]; String[] AwayTeam = new String[10]; int[] HomeScore = new int[10]; int[] AwayScore = new 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: ");
}
String[]HomeTeam=新字符串[10];
字符串[]AwayTeam=新字符串[10];
int[]HomeScore=新int[10];
int[]AwayScore=新int[10];
int指数=0;
整数和=0;
int sum1=0;
做
{
系统输出打印(“输入主队名称:”);
HomeTeam[index]=kbd.nextLine();
系统输出打印(“输入客场球队名称:”);
AwayTeam[index]=kbd.nextLine();
系统输出打印(“输入主队得分:”);
HomeScore[index]=kbd.nextInt();
系统输出打印(“输入客场球队得分:”;
AwayScore[index]=kbd.nextInt();
kbd.nextLine();
}指数<10;
指数=0;
System.out.println();
做
{
System.out.println(HomeTeam[索引]+“[”+HomeScore[索引]+“]”+“|”+AwayTeam[索引]+“[”+AwayScore[索引]+“]);
指数=指数+1;
}指数<10;
kbd.close();
对于(索引=0;索引<10;索引++)
总和+=HomeScore[指数];
对于(索引=0;索引<10;索引++)
sum1+=AwayScore[指数];
System.out.println();
系统输出打印项次(“总计”);
System.out.println(“------------------------------------”;
System.out.println(“播放的比赛总数:+索引”);
System.out.println(“所有家庭分数的总和:+总和”);
System.out.println(“所有客场得分总和:+sum1”);
System.out.println(“提款总数:”);
System.out.println(“最高分:”);
System.out.println(“客场得分最高:”);
}
}添加另一个名为
drawCount
的局部变量。循环遍历分数数组,并检查循环索引处两个数组中的元素是否相等。如果是,则将支取数增加一。然后在末尾打印
此外,您还可以将For
循环组合成一个。代码2中有For循环,但您只需要1即可获得所需的所有结果,因为所有数组的大小都是10
所以在这个循环中:
for(index = 0; index < 10; index++) {
// calculations
}
for(索引=0;索引<10;索引++){
//计算
}
在所有数组中使用索引
计算所有的总和、抽签数以及主场和客场的最高分数
还有一个建议:为变量使用更可读的名称,如:
sumMatches
,sumHomeScore
,SumDrawes
,sumHighestHome
,sumHighestAway
,sumHighestAway
,为了编写代码,您必须清楚您正在实施的解决方案。一个好的初学者练习,是写一个程序执行流程图(键盘前的纸)
我为您做了一个参考:
因此,考虑到该算法,我实现了一个可能的解决方案(使用硬编码数据)
主要类别:
编辑:
如果要避免空值,您必须询问每次迭代如果(homeTeam[index]!=null)
,还要手动计算匹配项(它们不再匹配数组长度)
句柄空值
注意:更好的选择是在请求输入时跳过空值。创建一个变量来存储绘图。循环十次。在循环索引中同时获取homescore和awayscore,如果它们相等,求和1以绘制计数变量。“我需要向我的导师解释我是如何做到的”-然后你们应该实际做到这一点,我们很高兴给出一些指示,但你们应该学会做什么和如何做。我没有发布任何代码,所以你们可以自己尝试。如果您仍然需要帮助,请发布您尝试过的内容,我可以编辑我的答案。int drawCount=0;if(HomeScore[索引]==AwayScore[索引])支取++;但这一个在结果中显示1,无论有多少drawshow要进行sumDraws,因为它必须检查两个数组中的元素?检查两个数组中的分数是否相等if(HomeScore[index]==AwayScore[index])…
感谢您的澄清我的代码看起来几乎一样它只是告诉我我的思维方式是正确的…谢谢大家…当然…当我有aaa[1]| aaa[1]null[0]| null[0]时,您能再回答一个问题吗?如何只打印输入的元素而不打印null元素?
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
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