大家好,我需要一些帮助。我必须上课Day和Weather每个班级都有自己的领域。 课日及其字段(日期,温度和评论)。天气及其领域(季节和评论)。这是主要问题:查找并显示以下数据:平均温度,最高温度的一天,最长评论的日期。所以我已经很容易地编写了这两个类的基本结构,但是有了逻辑结构,我请堆叠一些 建议或帮助如何解决或改进它我是编程新手。 这是课日:
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+"\":");
}
你会宣布哪种类型的日期? 请一些解释,因为这是我的应用程序的逻辑部分 对未来应用的影响 感谢每个人的耐心和理解
答案 0 :(得分:0)
平均温度应该更像是:
public int averageTemp()
{
int totalTemp=0;
for (Day day1 : day)
{
totalTemp+=day1.getTemperature;
}
return totalTemp/day.length;
}
对于最高温度,你对你所做的最长评论有正确的想法。只需使用相同的逻辑:循环遍历所有条目,将每个条目与目前为止找到的最高条目进行比较,如果这个条目更高,则到目前为止这是最高的。