自上周以来我一直在学习 Java(我有 C# 的基本知识),现在我正在练习课程。我不明白为什么会出现此异常,我尝试多次更改代码但没有任何效果。这是代码:
package javaapplication1;
import java.util.Scanner;
public class JavaApplication1 {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Competitor[] competitors;
System.out.println("Enter number of competitors");
competitors = Create(sc.nextInt());
for(int i = 0;i<competitors.length - 1;i++)
for(int j=i+1;j<competitors.length;j++)
if(competitors[i].GetHeight()==competitors[j].GetHeight())
System.out.println(competitors[i]+" and "
+competitors[j] + "bounced to height"
+competitors[i].GetHeight());
}
static Competitor[] Create(int n) {
Competitor[] competitors = new Competitor[n];
for(Competitor c : competitors) {
c = new Competitor();
System.out.println("Enter name, id and bounce height");
c.SetName(sc.next());
c.SetId(sc.next());
c.SetHeight(sc.nextFloat());
}
return competitors;
}
}
class Competitor {
private String name;
private String id;
private float height;
public Competitor() {
}
public Competitor(String name, String id, float height) {
this.name = name;
this.id = id;
this.height = height;
}
public String GetName() {
return this.name;
}
public String GetId() {
return this.id;
}
public Float GetHeight() {
return this.height;
}
public void SetName(String name) {
this.name = name;
}
public void SetId(String id) {
this.id = id;
}
public void SetHeight(Float height) {
this.height = height;
}
}
我在这一行得到了例外: if(competitors[i].GetHeight()==competitors[j].GetHeight())
这里有什么问题?
非常感谢!