-2

你好我叠了哪一种方式可以很好的解决下面的问题?我想检查或检索最长的评论我不知道你有一些吗?

public class Day {
    private Date date;
    private int temperature;
    private String comments ;
    Weather weather;
   Day( Date date, int temperature, String comments){
       this.date=date; 
            this.temperature=temperature;
       this.comments=comments;
   }
 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 String getComments(){
        return  comments;

    }
    public void setComments(String comments){
        this.comments=comments;
    }
    public Boolean longestComment(String longcom){
        StringTokenizer st=new StringTokenizer(comments);
       String s;
           st.countTokens();
        if(st.hasMoreTokens()){
            return true;
        }
        return  false;
    }

        public void testComent(String longcom){
            if  (longestComment(longcom)){
             System.out.println("The longest comment is:" +longcom);
            }
        }

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

             Day day=new Day(c.getTime(),20,"Today is normal temperature");
                day.testComent("Tomorrow is going to be the highest degree for ever in this summer");
           day.testComent("Yesterday was the coldest temperature");
           day.testComent("Next week is going to be the coldest temperature ever");
       }
}

我想检查哪个评论是最长的请一些建议

4

2 回答 2

0
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.Date;

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");
      System.out.println("the longest comment is:");
      System.out.println( day.longestComment() );
   }
}

输出:

the longest comment is: 
Tomorrow is going to be the highest degree for ever in this summer
于 2012-11-01T15:10:42.123 回答
0

通过这种方法,您最终可以获得具有最长评论的 Day 对象,并且您的 testComment 方法将打印最长的评论。那是你所期望的吗

public boolean longestComment(String longcom){

    if(longcom.length() > this.comments.length()){
        this.comments = longcom;
        return true;
    }
    return  false;
}
于 2012-11-01T15:03:29.780 回答