0

大家好,我需要一些帮助。我必须上课日和天气,每个课程都有自己的领域。上课日及其字段(日期、温度和评论)。类天气及其字段(季节和评论)。这是主要问题:查找并显示以下数据:平均温度、最高温度的日子、最长评论的日子。所以我已经很容易地编写了这两个类的基本结构,但是对于我堆叠的逻辑结构,请一些建议或帮助如何解决它或改进它我是编程新手。这是上课日:

public class Day {
    private Date date;
    private int temperature;
    private ArrayList<String> comments = new ArrayList<String>() ;
    Weather weather;

    Day( Date date, int temperature, String comment){
        this.date=date;
        this.temperature=temperature;
        this.comments.add(comment);
    }
    public Date getDate(){
        return  date;
    }
    public void setDate(Date date){
        this.date=date;
    }
    public int getTemperature(){
        return  temperature;

    }
    public  void setTemperature(int temperature){
        this.temperature=temperature;

    }
    public ArrayList<String> getComments() {
        return comments;

    }
    public void addComment(String comment){
        this.comments.add(comment);
    }
    public String longestComment() {
        int length = 0;
        String longestComment = "";
        for ( String comment : comments )
        {
            if ( comment.length() > length )
            {
                length = comment.length();
                longestComment = comment;
            }
        }
        return longestComment;
    }


    public  static  void main (String args []){
        Calendar c=new GregorianCalendar();

        Day day=new Day(c.getTime(),20,"Today is normal temperature");
        day.addComment("Tomorrow is going to be the highest degree for ever in this summer");
        day.addComment("Yesterday was the coldest temperature");
        day.addComment("Next week is going to be the coldest temperature ever");
        day.addComment("In the early years of the ice era there was only highest degree of temperature for ever");
        System.out.println("the longest comment is:");
        System.out.println( day.longestComment() );
    }
}

这里是天气类:

public class Weather {
private  String season;
    private String comments;
        Day [] day;

     public   Weather(String season, String comments, Day [] day){
         this.comments=comments;
         this.season=season;
         this.day=day;
       }

   public static void main (String args []){


   }
    public String getSeason(String season){
        return season;
    }
    public void setSeason(String season){
                    this.season=season;
    }
    public String getComments(String comments){
        return comments;
    }
          public void setComments(String comments){
              this.comments=comments;
          }
    public Day getDay(Day  day){
        return day ;
    }
    public void setDay(Day [] day){
        this.day=day;
    }
    public int averageTemp(int aver){
    for(int t=0; t<day.length;t++){
        if (aver==((day.length)/2)){

          return day[t].getTemperature();
    }
    }
        return 0;
    }
  public void maxTemp(){
      int max=0;

      for (int i=0; i<day.length;i++){

      }

  }
    public void findLongestComment(String comment){

   System.out.println("Comment\""+comment+"\":");
}

你会声明哪种类型的日期?请解释一下,因为这是我的应用程序的逻辑部分,它会影响应用程序的未来开发 感谢大家的耐心和理解

4

1 回答 1

0

平均温度可能更像是:

public int averageTemp()
{
  int totalTemp=0;
  for (Day day1 : day)
  {
    totalTemp+=day1.getTemperature;
  }
  return totalTemp/day.length;
}

对于最高温度,您对最长评论所做的事情有正确的想法。只需使用相同的逻辑:遍历所有条目,将每个条目与迄今为止找到的最高值进行比较,如果这个更高,则成为迄今为止的最高值。

于 2012-11-01T22:06:27.100 回答