私は先週から 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())
ここで何が問題なのですか?
どうもありがとう!