通过计算得分和进球差来计算 UEFA冠军联赛排名的程序,并完成了该程序,这似乎是正确的,但它没有接受输入。请有人告诉为什么会引发NullPointerException。我已经尝试了很多次,但是没有用。
它将字符串或行作为:HomeTeamName HomeTeamGoals vs AwayTeamGoals AwayTeamName 然后,如果团队名称已经存在,则它将存储在其对象中。 否则它将存储在一个新对象中。 一个组中只有4个团队,因此仅创建了4个团队对象。 一个组将有12个匹配项,即为什么循环条件为12。
class Team{
int point , gdiff ;
String name = new String();
Team(){
point = gdiff = 0;
name = "NA";
}}
class Group{
private Team[] team;
Group() {
this.team = new Team[4];
}
public void getData(){
System.out.println("Enter Group Data as :HomeTeamName HomeTeamGoals vs. AwayTeamGoals AwayTeamName");
Scanner scan = new Scanner(System.in);
String match;
for(int i=0;i<12;i++){
match = scan.nextLine();
String[] words = match.split(" ");
int flag01 =0,flag02 = 0;
for (Team team1 : team) {
//probleum statement starts here
//NullPointerException is arising
if (team1.name.equals(words[0])) {
team1.gdiff += calculateGDiff(Integer.parseInt(words[1].trim()),Integer.parseInt(words[4].trim()));
team1.point += calculatePoints(Integer.parseInt(words[1].trim()),Integer.parseInt(words[4].trim()));
flag01 = 1;
System.out.println(".....");
continue;
}
if (team1.name.equals(words[5])) {
team1.gdiff += calculateGDiff(Integer.parseInt(words[4].trim()),Integer.parseInt(words[1].trim()));
team1.point += calculatePoints(Integer.parseInt(words[4].trim()),Integer.parseInt(words[1].trim()));
flag02 = 1;
System.out.println(".....");
}
}
for (Team team1 : team) {
if (flag01 == 0) {
if (team1.name.equals("NA")) {
team1.name = words[0];
team1.gdiff += calculateGDiff(Integer.parseInt(words[1].trim()),Integer.parseInt(words[4].trim()));
team1.point += calculatePoints(Integer.parseInt(words[1].trim()),Integer.parseInt(words[4].trim()));
flag01 = 1;
System.out.println(".....");
}
}
if (flag02 == 0) {
if (team1.name.equals("NA")) {
team1.name = words[5];
team1.gdiff += calculateGDiff(Integer.parseInt(words[4].trim()),Integer.parseInt(words[1].trim()));
team1.point += calculatePoints(Integer.parseInt(words[4].trim()),Integer.parseInt(words[1].trim()));
flag02 = 1;
System.out.println(".....");
}
}
}
}
}
private int calculateGDiff(int goals, int goalsR){
return goals-goalsR;
}
private int calculatePoints(int goals, int goalsR){
if(goals>goalsR)
return 3;
else if(goals==goalsR)
return 1;
else
return 0;
}
}
我只是正确地存储了目标和名称并删除了NullPointerException。