【问题标题】:Compilation error in VPL but not in Intellij (Java, University Assignment)VPL 中的编译错误,但 Intellij 中没有(Java,大学作业)
【发布时间】:2020-12-01 19:56:55
【问题描述】:

我的问题是我的代码运行得非常好,并且在 Intellij 中可以满足我的要求,但在 VPL 中却不行。尝试在 tailLength 计算中将 int 转换为 double 时出现问题。

错误:java.lang.String,java.lang.String,int,int,double 找到:java.lang.String,java.lang.String,int,int 原因:实际参数列表和形式参数列表的长度不同。

所以似乎缺少双精度,这里是代码:

public class Dog {

    private String name;
    private String breed;
    private int age;
    private int weight;
    private double tailLength;



   public Dog(String name, String breed, int age, int weight, double tailLength) {
       this.name = name;
       this.breed = breed;
       this.age = age;
       this.weight = weight;
       this.tailLength = tailLength;
   }

    public String getName() {
        return name;
    }

    public String getBreed(){
       return breed;
    }

    public int getAge() {
       return age;
    }

    public void increaseAge(){
       age++;
    }

    public int getWeight() {
       return weight;
    }

    public double getTailLength() {
       if (getBreed().equals("Dachshund") || getBreed().equals("Tax")){
           tailLength = 3.7;
           return tailLength;
       } else {
           tailLength = (double)getAge() * ((double)getWeight() / 10.0);
           return tailLength;
       }
    }

    public String toString(){
      return getName() + " " + getBreed() + " " + getAge() + " " + getWeight()+ " " + getTailLength();
    }

}

【问题讨论】:

  • 那个类看起来不错,你能发布创建对象的代码吗?从错误消息来看,您似乎错过了构造函数的参数,尽管这在 IntelliJ 中如何工作当然很有趣。
  • 错误具体告诉你哪里有问题的行在哪里,并且不在这个类中。
  • @stridecolossus 是的,这就是问题所在,我们不允许看到创建对象的代码,它隐藏在 VPL (?) 中,这使得它更加混乱,因为我们应该创建上面的代码是我们老师的代码,我们不允许看到......所以我在想是否有另一种方法可以以另一种方式将整数转换为双精度数

标签: java constructor compiler-errors double data-conversion


【解决方案1】:

我怀疑你在这里搞错了?该错误表明 VPL(无论是什么)正在使用 四个 值(String、String、int、int)调用您的构造函数。没有 double 被传递,因此不需要转换它。

练习的重点是计算尾长,因此使用getTailLength() 方法。删除 tailLength 构造函数参数(以及多余的类成员)。

【讨论】:

  • 是的,做到了!谢谢一百万!
猜你喜欢
  • 1970-01-01
  • 2013-07-29
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-06
  • 2013-05-03
相关资源
最近更新 更多