需要帮助从不同类别获取物品的平均重量

  • 本文关键字:获取 帮助 同类 java
  • 更新时间 :
  • 英文 :


我被家庭作业的最后一部分卡住了,不知道该怎么完成。

我的任务是取3个重量(以磅和盎司为单位)并获得平均重量,然后将其打印到命令行。我现在的想法是把这三个权重加在一起,然后我认为这就像把它们除以3一样简单。然而,当我尝试这样做时,它会抛出一个错误,说"二元操作符'/' ">

的操作数类型不正确"。我有两个独立的文件。

"

// a class that represents a Weight in pounds and ounces
class Weight {
// private instant variables
private int pounds;
private double ounces;
// private constant variable
private static double OUNCES_IN_A_POUND = 16;

// a method that normalizes the weight on which it was invoked
private void normalize() {
// while loop used to continusoulsy loop til less than 16 ounces
while(ounces > OUNCES_IN_A_POUND) {
// decrement 16 ounces from the ounces and increment pound by 1
ounces -= OUNCES_IN_A_POUND;
pounds++;
}
}

// method used to return total number of ounces
private double toOunces() {
return pounds * OUNCES_IN_A_POUND + ounces;
}

// constructor for pounds and ounces to be initialized
public Weight(int pounds, double ounces) {
this.pounds = pounds;
this.ounces = ounces;
// normalize the ounces
normalize();
}

// a method that checks if the current object's weight is less than
public boolean lessThan(Weight wt) {
if(this.pounds < wt.pounds)
return true;
else if(this.pounds > wt.pounds)
return false;
else {
if(this.ounces < wt.ounces)
return true;
else
return false;
}
}  

// a method that adds weight to the current object
public void addTo(Weight wt) {
this.pounds += wt.pounds;
this.ounces += wt.ounces;
normalize();
}

// a method that returns the string 
public String toString() {
return pounds + " pounds " + String.format("%.2f", ounces) + " ounces";
}
}

第二文件:

class Project1 {
// a method that returns the smallest weight among 3 weights
private static Weight findMinimum(Weight w1, Weight w2, Weight w3) {
// if the first weight is smaller than the second & third weight
Weight minimum;    
if(w1.lessThan(w2) && w1.lessThan(w3)){
// return the first weight as the smallest weight
minimum = w1;
}
// else if the second weight is smaller than the first & third weight
else if (w2.lessThan(w1) && w2.lessThan(w3)){
// return the second weight as the smallest weight
minimum = w2;
}
// else the third weight is smaller than the first & second weight
else{
// return the third weight as the smallest weight
minimum = w3;
}
System.out.println("nThe minimum weight is " + minimum.toString());
return minimum;
}
// a method that returns the highest weight among 3 weights
private static Weight findMaximum(Weight w1, Weight w2, Weight w3) {
// if the first weight is greater than the second & third weight
Weight maximum;
if (!w1.lessThan(w2) && !w1.lessThan(w3)){
// return the first weight as the highest weight
maximum = w1;
}
// else if the second weight is greater than the first & third weight
else if (!w2.lessThan(w1) && !w2.lessThan(w3)){
// return the second weight as the highest weight
maximum = w2;
}
// else the third weight is greater than the first & second weight
else{
// return the third weight as the highest weight
maximum = w3;
}  
System.out.println("nThe maximum weight is " + maximum.toString());
return maximum;
}
// a method that returns the average weight among 3 weights
private static Weight findAverage(Weight w1, Weight w2, Weight w3) {
// create a new Weight object
Weight average = new Weight(0, 0);
// add the weight of the three objects to the new Weight object
// by invoking the addTo() method
average.addTo(w1);
average.addTo(w2);
average.addTo(w3);
average = average / 3;


System.out.println("nThe average weight is " + average.toString());
return average;
}


// main method
public static void main(String[] args) {
// create three weight objects
Weight weight1 = new Weight(25, 20);
Weight weight2 = new Weight(30, 8);
Weight weight3 = new Weight(23, 10);
// display the 3 weights
System.out.println("Weight 1: " + weight1.toString());
System.out.println("Weight 2: " + weight2.toString());
System.out.println("Weight 3: " + weight3.toString());
// invoke the findMinimum() method to get the smallest weight among the 3
// weights
// display the minimum of the 3 weights
Weight min = findMinimum(weight1, weight2, weight3);
//findMinimum;
// invoke the findMaximum() method to get the highest weight among the 3 weights
Weight max = findMaximum(weight1, weight2, weight3);
Weight avg = findAverage(weight1,weight2, weight3);

}
}

第二个文件是我需要帮助的。在findAverage方法中

因为这是家庭作业,所以我不会马上给你答案,因为它确实违背了家庭作业的目的。不过,我相信给你指明正确的方向会有所帮助。话虽如此,也许您可以尝试将数组与for循环一起使用。For循环可以用来找到总数,然后你可以通过数组的大小得到数字的数量。然后你可以执行total/size of array来得到平均值。我希望这对你有帮助!

最新更新