Java 编写计算长度/平均值的方法

Java 编写计算长度/平均值的方法,java,class,methods,Java,Class,Methods,我正试图编写一个包含main所需的所有方法的类,但在弄清楚如何使用这些方法确定所需的值时遇到了困难。我遇到的问题是getAverageLength和getCount 电流输出: The length of Line 1 is 1.0 The length of Line 2 is 10.0 There are 0 lines and the average length is 0 The length of Line 1 is 1.0 The length of Line 2 is 10.0 T

我正试图编写一个包含main所需的所有方法的类,但在弄清楚如何使用这些方法确定所需的值时遇到了困难。我遇到的问题是getAverageLength和getCount

电流输出:

The length of Line 1 is 1.0
The length of Line 2 is 10.0
There are 0 lines and the average length is 0
The length of Line 1 is 1.0
The length of Line 2 is 10.0
The length of Line 3 is 7.0
There are 0 lines and the average length is 0
预期产出:

The length of Line 1 is 1
The length of Line 2 is 10
There are 2 lines and the average length is 5.5
The length of Line 1 is 7
The length of Line 2 is 10
The length of Line 3 is 7
There are 3 lines and the average length is 8.0
这是我正在使用的主要方法的一部分

public class TestParts {

public static void main(String[] args) {

     MyLine ml1 = new MyLine();
     MyLine ml2 = new MyLine(10);
     System.out.println("The length of Line 1 is " + ml1.getLength());
     System.out.println("The length of Line 2 is " + ml2.getLength());
     System.out.println("There are " + MyLine.getCount() +
     " lines and the average length is " + MyLine.getAverageLength());
     MyLine ml3 = new MyLine(7);
     ml1.setLength(7);
     System.out.println("The length of Line 1 is " + ml1.getLength());
     System.out.println("The length of Line 2 is " + ml2.getLength());
     System.out.println("The length of Line 3 is " + ml3.getLength());
     System.out.println("There are " + MyLine.getCount() +
     " lines and the average length is " + MyLine.getAverageLength());
    }
}
下面是我写的一个单独的类,用来计算这些值

class MyLine {
private double getLength;

MyLine() {
    getLength = 1;
}

double getLength() {
    return getLength;
}

MyLine(double setLength) {
    getLength = setLength;
}

public void setLength(int i) {
    getLength = getLength();
}

public static int getCount() {

    return 0;
}

public static int getAverageLength() {

    return 0;
}

}

对于
getCount
,创建一个由每个构造函数递增的
static int


对于
getAverageLength
,使用每个构造函数添加到的行的总和生成一个
static int
,并将其除以计数。

代码有几个问题。 首先,建议在注释中记录这些方法应该做什么。 这将帮助你和其他人。 第二,这些方法:

public void setLength(int i) {
    getLength = getLength();
}
getLength
私有成员变量的名称可能不正确。 我怀疑意图是一个类似于
length
的名词,而
getLength()
是一个用于返回当前
length
的方法。 此方法使用原语
int
数据类型来设置
double
的类型。 这是故意的吗? 看起来是个误解。 此外,它没有函数,因为它只是将
getLength
(同样,应该是
length
)变量设置为方法
getLength()
的返回值,该方法反过来返回
getLength
的值。这是一个补救逻辑和基本数学问题: A=1=getLength()=A=1

为什么期望从这个方法返回零以外的任何东西

公共静态int getAverageLength(){

}

这里也一样。。。基本逻辑问题

我不会在论坛上发布此类问题,因为在提出问题之前,论坛没有做基本的准备。

构建一个
HashMap
整数是MyLine的长度

您只需将那些
MyLine
对象放入该地图即可

{ml1:0,
ml2:10,
ml3:7}


然后你可以计算你想要的一切。

你的预期和实际输出是什么?我只是在问题中添加了实际和预期。你的setLength方法没有任何作用。事实上,你的类有太多问题,我会放弃它,重新开始。谢谢你的建议,但我不知道怎么做。在课堂上,做一个
静态int count
和一个
静态int sum
。然后,在每个构造函数中,添加
count++
sum+=getLength位于底部。0是占位符,只是为了显示方法,认为这很明显。问题说明实际结果与预期结果:有0行,平均长度为0。。。与预期相比:共有2条线,平均长度为5.5。如果这些值被显式设置为零,那么预期结果怎么可能不是零呢?
public static int getCount() {

    return 0;
}
return 0;