当前位置: 首页 > 知识库问答 >
问题:

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

何兴邦
2023-03-14

我被作业的最后一部分卡住了,只是不知道如何完成它。

我的任务是取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方法中。

共有1个答案

简烨烁
2023-03-14

由于这是家庭作业,我不会马上给你答案,因为它确实违背了家庭作业的目的。但是,我相信向您指出正确的方向会有所帮助。话虽如此,也许您可以尝试使用带有for循环的数组。for循环可用于查找总数,然后您可以通过数组的大小获得数字的数量。然后您可以执行数组的总数/大小以获得平均值。我希望这有帮助!

 类似资料:
  • 我似乎无法从用户输入的分数中计算出平均分数。我也不能让它停止例外输入大于100或小于0。有人能告诉我我做错了什么吗?谢谢

  • 问题内容: 我有以下示例字符串 我只对 test6IAmInterestedIn 感兴趣,它位于第四个斜杠之后,可以包含3个字符,多个字母并以3个数字结尾,即。 我想在上述正则表达式中添加的内容是,我总是在匹配的第四个斜杠之后选择字符串。我该怎么办? 问题答案: 你可以试试这个演示 哪里 匹配包含正斜杠的4个块 捕捉您想要的图案

  • 我是Hadoop Map/Reduce的新手。我正在尝试编写一个Map/Reduce作业,以查找n个进程所花费的平均时间,给定一个输入文本文件,如下所示: 我读了一些教程,但仍然不能完全理解。我的mapper和reducer类应该如何解决这个问题?我的输出将始终是文本文件,还是可以直接将平均值存储在某种变量中? 谢了。

  • 我正在努力检索基于内容所有者的YouTube分析数据。从这次讨论(获取当前用户的YouTube内容所有者id)中,我了解到我需要查询youtubePartner.contentOwners.list(fetchmine=true)来获取内容所有者信息,其中的Id将被找到。 我纠结的是,当我调用youtubepartner . content owners . list(fetch mine = t

  • 声明如下: 我是XSL的新手。我知道href属性需要URI,但是href值如何在上面的语句中解析为URI。此代码是DITA-OT中xhtml插件xsl的一部分。有多个这样的语句。这些是来自根目录的相对路径吗?DITA-OT代码如何解析这些路径?

  • 我正在尝试解决hackerrank中的一个“几乎已排序”的挑战。问题是: 给定一个包含元素的数组,可以只使用以下操作之一按升序对该数组进行排序吗? 交换两个元素。反转一个子段。 输入格式 第一行包含一个整数,指示数组的大小。 下一行包含以空格分隔的整数。 样本输入#1 2 4 2 示例输出 #1 是< br >交换1 2 示例输入 #2 3 3 1 2 样品输出#2 不 示例输入 #3 6 1 5