我上周以来一直在学习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;
}
}
我在这一行得到了例外: 如果(竞争对手[I] .GetHeight()==竞争对手[j]的.GetHeight())
这里有什么问题?
非常感谢!
答案 0 :(得分: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#方法中,name以一个大写字母char开头,在Java中,它们以小写字母开头。
答案 1 :(得分: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#有不同的约定。请参阅here。
方法应该是动词,混合大小写,首字母小写,每个内部单词的首字母大写。
答案 2 :(得分: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;
但是就像那样,您只需将新竞争者分配给&#34; c&#34;。你应该取消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()
正在挑选原始值并将其置于类型类型Float中(自动感谢auto-boxing)。类类型在内部将float原语存储在字段中。当你使用==时,它比较了两个对象并确定它们没有指向同一个地方,即使它们的字段指向具有相同值的浮点基元。
这就是为什么在比较非原始类型时我们应该始终使用.equals()
方法而不是==,除非您尝试针对null进行测试。 .equals()
将确保它不会比较变量所指向的位置,而是比较它们的内部字段。
看一下这个答案https://stackoverflow.com/a/73021/2576857
我认为这有助于您了解自己遇到的两个问题。