我应该从数字序列中得到最小和最大的数字。我知道每种方法的方法体可能都不正确。有什么帮助吗?
public class DataSet
{
private int sum;
private int count;
private int largest;
private int smallest;
public DataSet()
{
sum = 0;
count = 0;
largest = 0;
smallest = 0;
}
public void addValue(int x)
{
sum = sum + x;
count++;
}
public int getSum()
{
return sum;
}
public double getAverage()
{
double average = sum/count;
return average;
}
public int getLargest()
{
int largest = Integer.MAX_VALUE;//not giving right answer, don't know how to use
return largest;
}
public int getSmallest()
{
int smallest = Integer.MIN_VALUE;//not giving right answer, don't know how to use
return smallest;
}
}
如果需要,这是测试器类,尽管我认为问题不在于这里。
public class DataSetTester
{
public static void main(String[] args)
{
DataSet myData = new DataSet();
myData.addValue(100);
myData.addValue(5);
myData.addValue(4);
myData.addValue(2);
System.out.println("Sum = " + myData.getSum());
System.out.println("Average = " + myData.getAverage());
System.out.println("Largest value = " + myData.getLargest());
System.out.println("Smallest value = " + myData.getSmallest());
}
}