这项任务的基本思想是创建一个程序,该程序可以提供摘要并识别谁赢得了#34;匹配"在体育赛事(足球,篮球,足球,棒球等)
**This is my code:**
`import java.util.Scanner;
public class Team {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Ask questions about the game type etc.
System.out.println("Please enter game name: ");
String gameName = sc.next();
System.out.println("Please enter " + gameName + " team 1 name: ");
String t1N = sc.next();
System.out.println("Please enter " + gameName + " team 2 name: ");
String t2N = sc.next();
System.out.println("What is a score in " + gameName + " called? ");
String scoreName = sc.next();
System.out.println("How many points per " + scoreName + " in " + gameName + "?");
int scoreValue = sc.nextInt();
System.out.println("What is a period in " + gameName + " called?");
String periodName = sc.next();
System.out.println("How many " + periodName + " in " + gameName + "?");
int numberOfPeriods = sc.nextInt();
int sum1 = 0;
int sum2 = 0;
for (int i = 1; i <= numberOfPeriods; i++) {
System.out.println(periodName + " #" + i);
System.out.println("How many " + scoreName + " for " + t1N + "?");
int numberOfScoresT1[] = new int[sc.nextInt()];
System.out.println("How many " + scoreName + " for " + t2N + "?");
int numberOfScoresT2[] = new int[sc.nextInt()];
for (int counter = 0; counter < numberOfScoresT1.length; counter++)
sum1 += numberOfScoresT1[counter];
for (int counter = 0; counter < numberOfScoresT1.length; counter++)
sum2 += numberOfScoresT2[counter];
}
System.out.println("Team 1 scored " + sum1 + " team 2 scored " + sum2);
}`
这是我收到的错误: 请输入游戏名称: 足球 请输入足球队1名: 海豚 请输入足球队2名称: 美洲虎 什么是Fotball的得分叫什么? 接地 在Fotball中每次触地得分多少分? 7 什么是Fotball中的一个时期? 25美分硬币 Fotball多少个季度? 4 第1季 海豚的触地得分有多少? 3 美洲虎有多少触地得分? 2 线程&#34; main&#34;中的例外情况java.lang.ArrayIndexOutOfBoundsException:2 在Team.main(Team.java:36)
我认识到我使用的阵列都在for循环中,我认为这是造成问题的原因,但我不确定如何修复它。
这是一个示例输出应该看起来像: 第1季: 海豚有多少次达阵? 2 充电器有多少次达阵? 1
第二季度: 海豚有多少次达阵? 0 充电器有多少次达阵? 1
第3季: 海豚有多少次达阵? 0 充电器有多少次达阵? 2
第四季度: 海豚有多少次达阵? 3 充电器有多少次达阵? 0
海豚队以35分的成绩获得5次达阵 充电器以28分的成绩获得4次达阵
海豚以7分获胜!
答案 0 :(得分:1)
for (int counter = 0; counter < numberOfScoresT1.length; counter++)
sum2 += numberOfScoresT2[counter];
循环的第二个参数是
for (int counter = 0; counter < numberOfScoresT2.length; counter++)
看到你正在访问正文中的numberOfScoresT2数组。
答案 1 :(得分:0)
你的内部for循环应该如下
for (int counter = 0; counter < numberOfScoresT1.length; counter++)
sum1 += numberOfScoresT1[counter];
for (int counter = 0; counter < numberOfScoresT2.length; counter++)
sum2 += numberOfScoresT2[counter];
你在第二个内部for循环中使用了数组numberOfScoresT1的长度而不是数组numberOfScoresT2。
import java.util.Scanner;
public class Team {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Ask questions about the game type etc.
System.out.println("Please enter game name: ");
String gameName = sc.next();
System.out.println("Please enter " + gameName + " team 1 name: ");
String t1N = sc.next();
System.out.println("Please enter " + gameName + " team 2 name: ");
String t2N = sc.next();
System.out.println("What is a score in " + gameName + " called? ");
String scoreName = sc.next();
System.out.println("How many points per " + scoreName + " in " + gameName + "?");
int scoreValue = sc.nextInt();
System.out.println("What is a period in " + gameName + " called?");
String periodName = sc.next();
System.out.println("How many " + periodName + " in " + gameName + "?");
int numberOfPeriods = sc.nextInt();
int sum1 = 0;
int sum2 = 0;
int numberOfScoresT1[] = new int[numberOfPeriods];
int numberOfScoresT2[] = new int[numberOfPeriods];
for (int i = 0; i <numberOfPeriods; i++) {
System.out.println(periodName + " #" + i);
System.out.println("How many " + scoreName + " for " + t1N + "?");
numberOfScoresT1[i] = sc.nextInt();
System.out.println("How many " + scoreName + " for " + t2N + "?");
numberOfScoresT2[i] = sc.nextInt();
}
sc.close();
for (int counter = 0; counter < numberOfPeriods; counter++) {
sum1 += numberOfScoresT1[counter];
sum2 += numberOfScoresT2[counter];
}
System.out.println("Team 1 scored " + sum1 + " team 2 scored " + sum2);
}
}