• 主页

# 请大家帮我看看这道题

linsenlinwei 2010-10-29 03:48:05
class Date{
int year;
int mouth;
int day;
Date(int _year,int _mouth, int _day){
year=_year;
mouth=_mouth;
day=_day;
}

public String toString() {
return "Year:Month:Day -- " + year + "-" + mouth + "-" + day;
}
public int compare(Date d){
return this.year>d.year ? 1
: this.year<d.year ? -1
: this.mouth>d.mouth ? 1
: this.mouth<d.mouth ? -1
: this.day>d.day ? 1
: this.day<d.day ? -1 :0;
}
}

public class TestDateSort2{
public static void main(String args[]){
String temp;
Date days[]=new Date[6];
days[0]=new Date(2001, 11, 2);
days[1]=new Date(2009,10, 2);
days[2]=new Date(2008, 2, 2);
days[3]=new Date(2007, 3, 3);
days[4]=new Date(2006, 4, 5);
for(int i=0;i<days.length;i++){
/11/ temp=days[i].toString();
System.out.println(temp);

}
selectSort(days);
for(int i=0;i<days.length;i++){
temp=days[i].toString();
System.out.println(temp);
}

}

public static void selectSort(Date a[]){
Date temp=a[0];
for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
if(a[j-1].compare(a[j])>0){
temp=a[j];

}

}
if(!(a[i].equals(temp))){
a[i]=temp;
}
}
}
}

at TestDateSort2.selectSort(TestDateSort2.java:51)
at TestDateSort2.main(TestDateSort2.java:39)

...全文
54 点赞 收藏 7

7 条回复

linsenlinwei 2010-10-29

linsenlinwei 2010-10-29
class Date{
int year;
int mouth;
int day;
Date(int _year,int _mouth, int _day){
year=_year;
mouth=_mouth;
day=_day;
}

public String toString() {
return "Year:Month:Day -- " + year + "-" + mouth + "-" + day;
}
public int compare(Date d){
return this.year>d.year ? 1
: this.year<d.year ? -1
: this.mouth>d.mouth ? 1
: this.mouth<d.mouth ? -1
: this.day>d.day ? 1
: this.day<d.day ? -1 :0;
}

/* public boolean equals(Date d){
if((this.year==d.year)&&(this.mouth==d.mouth)&&(this.day==d.day)){
return true;
}else {return false;}
}*/
}

public class TestDateSort2{
public static void main(String args[]){
String temp;
Date days[]=new Date[5];
days[0]=new Date(2000, 11, 2);
days[1]=new Date(2009,10, 2);
days[2]=new Date(2008, 2, 2);
days[3]=new Date(2007, 3, 3);
days[4]=new Date(2006, 4, 5);
for(int i=0;i<days.length;i++){
temp=days[i].toString();
System.out.println(temp);

}
System.out.println();
selectSort(days);
for(int i=0;i<days.length;i++){
temp=days[i].toString();
System.out.println(temp.toString());
}

}

public static void selectSort(Date a[]){
for(int i=0;i<a.length;i++){
Date temp;
temp=a[i];
int last=0;;

for(int j=i+1;j<a.length;j++){
if(temp.compare(a[j])>0){
temp=a[j];
last=j;
}
}

if((a[i].equals(temp))==false){
a[last]=a[i];
a[i]=temp;
}
}
}

}

zheng192004 2010-10-29
/11/ temp=days[i].toString();

Date days[]=new Date[6];
days[0]=new Date(2001, 11, 2);
days[1]=new Date(2009,10, 2);
days[2]=new Date(2008, 2, 2);
days[3]=new Date(2007, 3, 3);
days[4]=new Date(2006, 4, 5);

dinglimin2009 2010-10-29
``````Date(int _year,int _mouth, int _day){
this.year=_year;//楼主此处最好这样改过来
this.mouth=_mouth;
this.day=_day;
}

``````

sunyiz 2010-10-29
Date days[]=new Date[6];
days[0]=new Date(2001, 11, 2);
days[1]=new Date(2009,10, 2);
days[2]=new Date(2008, 2, 2);
days[3]=new Date(2007, 3, 3);
days[4]=new Date(2006, 4, 5);

kebin0001 2010-10-29
Date days[]=new Date[6];
days[0]=new Date(2001, 11, 2);
days[1]=new Date(2009,10, 2);
days[2]=new Date(2008, 2, 2);
days[3]=new Date(2007, 3, 3);
days[4]=new Date(2006, 4, 5);

11行注釋的話，temp就沒初始化，編譯不過。

ScAREcrOw_ss 2010-10-29

Java SE

6.1w+

Java 2 Standard Edition