1

自上周以来我一直在学习 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())

这里有什么问题?

非常感谢!

4

3 回答 3

2

Create 方法以错误的方式初始化竞争者值。尝试这个:

    for(int i = 0;i<competitors.length;i++){
        competitors[i] = new Competitor();
        System.out.println("Enter name, id and bounce height");
        competitors[i].SetName(sc.next());
        competitors[i].SetId(sc.next());
        competitors[i].SetHeight(sc.nextFloat());
    }

还有一个建议。虽然在 C# 方法名称以一个大写字符开头,但在 Java 中它们以小写字母开头。

于 2013-07-13T11:35:26.203 回答
0

你在做这个

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;

但是像这样,您只需为“c”分配一个新的竞争对手。你应该把它脱掉,c = new Competitor()所以它是这样的

Competitor[] competitors = new Competitor[n];
for(Competitor c : competitors) {
   System.out.println("Enter name, id and bounce height");
   c.SetName(sc.next());
   c.SetId(sc.next());
   c.SetHeight(sc.nextFloat());
}
return competitors;

您的另一个问题SetHeight()是因为SetHeight()选择了一个原始值并将其放入 Class 类型 Float (自动感谢auto-boxing)。类类型内部将浮点原语存储在字段中。当您使用 == 时,它比较了两个对象并确定它们没有指向同一个地方,即使它们的字段指向具有相同值的浮点原语。

这就是为什么在比较非原始类型时,我们应该始终使用.equals()方法而不是 ==,除非您尝试针对 null 对其进行测试。.equals()将确保它不会比较变量指向的位置,而是比较它们的内部字段。

看看这个答案https://stackoverflow.com/a/73021/2576857

我认为这将帮助您了解您遇到的这两个问题。

于 2013-07-13T12:17:48.817 回答
0

这增强了 for 循环:

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());
}

相当于:

for(int i = 0; i < competitors.length; i++) {
    Competitor c = competitors[i];
    c = new Competitor();
    System.out.println("Enter name, id and bounce height");
    c.SetName(sc.next());
    c.SetId(sc.next());
    c.SetHeight(sc.nextFloat());
}

所以你修改了c,但你从未修改过competitors


Java 与 C# 有不同的约定。见这里

方法应该是动词,混合大小写,首字母小写,每个内部单词的首字母大写。

于 2013-07-13T11:35:52.800 回答