找到最高和最低值

时间:2017-09-29 14:39:45

标签: java class logic

我必须完成这项任务。它看起来不错,除了getHighest输出和getLowest结果。这是我的代码:

KNW_CourseGradesV2班级:

public class KNW_CourseGradesV2 implements KNW_Analyzable
{
  //Declare array variable
  private KNW_GradedActivity[] grades = new KNW_GradedActivity[4];
  String results;

  /**
   * Constructor
   * */
  public KNW_CourseGradesV2()
  {

  }

  public void setLab(KNW_GradedActivity lab)
  {
    grades[0] = lab;
  }

  public void setPassFailExam(KNW_GradedActivity pfe)
  {
    grades[1] = pfe;
  }

  public void setEssay(KNW_GradedActivity essay)
  {
    grades[2] = essay;
  }
  public void setFinalExam(KNW_FinalExam fe)
  {
    grades[3] = fe;
  }
  public void setTotalGrade(KNW_GradedActivity tg)
  {
    grades[4] = tg;
  }

  public String toString()
  {
    //Lay out all of the scores
    return "Lab Score: " + grades[0].getScore() +
      "\tGrade: " + grades[0].getGrade() + 
      "\nPass/Fail Exam: " + grades[1].getScore() +
      "\tGrade: " + grades[1].getGrade() +
      "\nEssay Score: " + grades[2].getScore() + 
      "\tGrade: " + grades[2].getGrade() + 
      "\nFinal Exam Score: " + grades[3].getScore() + 
      "\tGrade: " + grades[3].getGrade();
  }

  //Find the average of the grades
  public double getAverage()
  {
    double sum = 0.0;
    double average = 0.0;
    double total = 0.0;

    //For loop to calc. the average
    for(int i =0; i<grades.length; i++)
    {
      total =+ grades[i].getScore();
      average = sum/grades.length;
    }
    return average;
  }

  public KNW_GradedActivity getHighest()
  {
    double highest = grades[0].getScore();

    for(int i = 0; i < grades.length;  i++)
    {
      if(grades[i].getScore() > highest)
      {
        highest = grades[i].getScore();
      }
    }

    //Created KNW_GradedActivity object to bring the highest number
    //into the setScore method so it can be a double
    KNW_GradedActivity gaH =  new KNW_GradedActivity();
    gaH.setScore(highest);
    return gaH;
  }

  public KNW_GradedActivity getLowest()
  {
    double lowest = grades[0].getScore();

    for(int i = 0; i < grades.length; i++)
    {
      if(grades[i].getScore() > lowest)
      {
        lowest = grades[i].getScore();
      }
    }
    //Samething here, just doing it for the lowest number
    KNW_GradedActivity gaL =  new KNW_GradedActivity();
    gaL.setScore(lowest);
    return gaL;
  }
}

KNW_Analyzable Class:

public interface KNW_Analyzable
{
  double getAverage();
  KNW_GradedActivity getHighest();
  KNW_GradedActivity getLowest();
}

KNW_GradedActivity类:

public class KNW_GradedActivity
{
   private double score;  // Numeric score

   /**
      The setScore method sets the score field.
      @param s The value to store in score.
   */

   public void setScore(double s)
   {
      score = s;
   }

   /**
      The getScore method returns the score.
      @return The value stored in the score field.
   */

   public double getScore()
   {
      return score;
   }

   /**
      The getGrade method returns a letter grade
      determined from the score field.
      @return The letter grade.
   */

   public char getGrade()
   {
      char letterGrade;

      if (score >= 90)
         letterGrade = 'A';
      else if (score >= 80)
         letterGrade = 'B';
      else if (score >= 70)
         letterGrade = 'C';
      else if (score >= 60)
         letterGrade = 'D';
      else
         letterGrade = 'F';

      return letterGrade;
   }
}

这是我得到的输出。我将只展示我面临的问题,不会显示所有输出。

输出:

Average: 0.0
Highest: KNW_GradedActivity@565d3fa7
Lowest: KNW_GradedActivity@655c4395> 

我已经尝试了大约一个星期来解决这个问题并找到正在发生的事情,我似乎无法找到它。

3 个答案:

答案 0 :(得分:1)

请注意,在返回方法时,您实际上正在返回imports: [ IonicPageModule.forChild(LoginPage), TranslateModule.forChild(), gaH,这两者都是KNW_GradedActivity对象。如果您想查看分数,则应该返回gaLgaH.getScore()

答案 1 :(得分:1)

为什么不使用<article class="items"> <section class="item"> <h2 class="item__title">Lorem ipsum dolor sit amet</h2> <p class="item__description">Lorem ipsum dolor sit amet.</p> <a class="item__more-button">See more</a> </section> <section class="item"> <h2 class="item__title">Lorem ipsum dolor sit amet</h2> <p class="item__description">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <a class="item__more-button">See more</a> </section> <section class="item"> <h2 class="item__title">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt</h2> <p class="item__description">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <a class="item__more-button">See more</a> </section> </article>Stream来获取统计数据?让api做所有的计算:

DoubleSummaryStatistics

答案 2 :(得分:0)

另一种方法是覆盖KNW_GradedActivity类中的toString方法:

@Override
public String toString() {
    return "{" +
            "score=" + score +
            ", grade='" + getGrade() + '\'' +
            '}';
}

通过这种方式,您无需更改实际的打印声明,并且可以将您的分数和成绩结合起来。它看起来像:

  

{得分= 90.0,等级=&#39; A&#39;}