有一个不知道哪里错的java程序 请高手们看下 老是提示说:无法从静态上下文中引用非静态变量。很郁闷
publicclassTestBubble{publicstaticvoidmain(String[]args){Date[]days=newDate[5];days[0...
public class TestBubble
{
public static void main(String[] args)
{ Date[] days = new Date[5];
days[0] = new Date(2008, 01, 02);//老是这点报错
days[1] = new Date(2007, 02, 03);
days[2] = new Date(2005, 03, 04);
days[3] = new Date(2006, 04, 05);
days[4] = new Date(2007, 05, 06);
Bubblesort(days);
for(int i=0;i<days.length;i++)
{
System.out.println(days[i]);
}
}
public static Date[] Bubblesort(Date[] a)
{int len = a.length;
for(int i=len-1;i>=1;i--)
{for(int j=0;j<i-1;j++)
{if(a[j].compare(a[j+1])>0)
{
Date temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return a;
}
class Date
{
int year,month,day;
Date(int y,int m,int d)
{
year=y;month=m;day=d;
}
public int compare(Date date)
{
return year>date.year? 1
:year<date.year? -1
:month>date.month? 1
:month<date.month? -1
:day>date.day? 1
:day<date.day? -1 : 0 ;
}
public String toString()
{
return "Year:Month:Day -- " + year + "-" + month + "-" + day;
}
}
}
一楼说的不行 我试过了 。 展开
{
public static void main(String[] args)
{ Date[] days = new Date[5];
days[0] = new Date(2008, 01, 02);//老是这点报错
days[1] = new Date(2007, 02, 03);
days[2] = new Date(2005, 03, 04);
days[3] = new Date(2006, 04, 05);
days[4] = new Date(2007, 05, 06);
Bubblesort(days);
for(int i=0;i<days.length;i++)
{
System.out.println(days[i]);
}
}
public static Date[] Bubblesort(Date[] a)
{int len = a.length;
for(int i=len-1;i>=1;i--)
{for(int j=0;j<i-1;j++)
{if(a[j].compare(a[j+1])>0)
{
Date temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return a;
}
class Date
{
int year,month,day;
Date(int y,int m,int d)
{
year=y;month=m;day=d;
}
public int compare(Date date)
{
return year>date.year? 1
:year<date.year? -1
:month>date.month? 1
:month<date.month? -1
:day>date.day? 1
:day<date.day? -1 : 0 ;
}
public String toString()
{
return "Year:Month:Day -- " + year + "-" + month + "-" + day;
}
}
}
一楼说的不行 我试过了 。 展开
7个回答
展开全部
你的Date是内部类,需要有外部类的对象才可以实例化
或者不要写成内部类就没问题了
可以这样该: public static void main(String[] args)
{ TestBubble t=new TestBubble();
Date[] days = new Date[5];
days[0] = t.new Date(2008, 01, 02);//老是这点报错
days[1] = t.new Date(2007, 02, 03);
......
或者:直接在class Data前面加个大括号,去掉最后的大括号 保证这个类是外部类 不懂再Hi我吧
或者不要写成内部类就没问题了
可以这样该: public static void main(String[] args)
{ TestBubble t=new TestBubble();
Date[] days = new Date[5];
days[0] = t.new Date(2008, 01, 02);//老是这点报错
days[1] = t.new Date(2007, 02, 03);
......
或者:直接在class Data前面加个大括号,去掉最后的大括号 保证这个类是外部类 不懂再Hi我吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
将Date声明为嵌套类,即静态的内部类即可,所以加一个关键字static
....
static class Date
{
int year,month,day;
Date(int y,int m,int d)
{
year=y;month=m;day=d;
}
.....
....
static class Date
{
int year,month,day;
Date(int y,int m,int d)
{
year=y;month=m;day=d;
}
.....
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码中大括号有问题,我改了下,可以运行了,自己看下是哪个括号。
public class TestBubble
{
public static void main(String[] args)
{
Date[] days = new Date[5];
days[0] = new Date(2008,1,2);//老是这点报错
days[1] = new Date(2007, 2, 3);
days[2] = new Date(2005, 3, 4);
days[3] = new Date(2006, 4, 5);
days[4] = new Date(2007, 5, 6);
Bubblesort(days);
for(int i=0;i<days.length;i++)
{
System.out.println(days[i]);
}
}
public static Date[] Bubblesort(Date[] a)
{
int len = a.length;
for(int i=len-1;i>=1;i--)
{for(int j=0;j<i-1;j++)
{if(a[j].compare(a[j+1])>0)
{
Date temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return a;
}
}
class Date
{
int year,month,day;
public Date(int y,int m,int d)
{
year=y;month=m;day=d;
}
public int compare(Date date)
{
return year>date.year? 1
:year<date.year? -1
:month>date.month? 1
:month<date.month? -1
:day>date.day? 1
:day<date.day? -1 : 0 ;
}
public String toString()
{
return "Year:Month:Day -- " + year + "-" + month + "-" + day;
}
}
public class TestBubble
{
public static void main(String[] args)
{
Date[] days = new Date[5];
days[0] = new Date(2008,1,2);//老是这点报错
days[1] = new Date(2007, 2, 3);
days[2] = new Date(2005, 3, 4);
days[3] = new Date(2006, 4, 5);
days[4] = new Date(2007, 5, 6);
Bubblesort(days);
for(int i=0;i<days.length;i++)
{
System.out.println(days[i]);
}
}
public static Date[] Bubblesort(Date[] a)
{
int len = a.length;
for(int i=len-1;i>=1;i--)
{for(int j=0;j<i-1;j++)
{if(a[j].compare(a[j+1])>0)
{
Date temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
return a;
}
}
class Date
{
int year,month,day;
public Date(int y,int m,int d)
{
year=y;month=m;day=d;
}
public int compare(Date date)
{
return year>date.year? 1
:year<date.year? -1
:month>date.month? 1
:month<date.month? -1
:day>date.day? 1
:day<date.day? -1 : 0 ;
}
public String toString()
{
return "Year:Month:Day -- " + year + "-" + month + "-" + day;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
Date[] days = new Date[5];
try{
Date d = fmt.parse("2008-12-11"); //把String转成Date..new Date(String)这个方法过时了。。
days[0]=d;
}catch(Exception e){
e.printStackTrace();
}
Date[] days = new Date[5];
try{
Date d = fmt.parse("2008-12-11"); //把String转成Date..new Date(String)这个方法过时了。。
days[0]=d;
}catch(Exception e){
e.printStackTrace();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把这个内部类独立出去
class Date
{
int year,month,day;
Date(int y,int m,int d)
{
year=y;month=m;day=d;
}
public int compare(Date date)
{
return year>date.year? 1
:year<date.year? -1
:month>date.month? 1
:month<date.month? -1
:day>date.day? 1
:day<date.day? -1 : 0 ;
}
public String toString()
{
return "Year:Month:Day -- " + year + "-" + month + "-" + day;
}
}
就可以了
class Date
{
int year,month,day;
Date(int y,int m,int d)
{
year=y;month=m;day=d;
}
public int compare(Date date)
{
return year>date.year? 1
:year<date.year? -1
:month>date.month? 1
:month<date.month? -1
:day>date.day? 1
:day<date.day? -1 : 0 ;
}
public String toString()
{
return "Year:Month:Day -- " + year + "-" + month + "-" + day;
}
}
就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询