看看这有什么错误? 呵呵,你这个例子中的Rectangle恐怕是这段代码的原来类名称吧,看得出来它应该是构建函数,所以建议把它改为test2,或者把类名改为Rectangle 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先数据封装性不好其次方法最好用类的实例来调用试试这样行不行class test2{ protected int width,height; public Rectangle(int w,int h) { width=w; height=h; } protected int GetArea() { int area; area=width*height; return area; } public void drawRect() { for(int i=width;i>0;i--) System.out.print("*"); System.out.println("\n"); for(int i=height-2;i>0;i--) { System.out.print("*"); for(int j=width-2;j>0;j--) System.out.print(' '); System.out.print("\n"); } for(int i=width;i>0;i--) System.out.print("*"); System.out.print("*"); }} Rectangle如果作为class中得一个方法,就要有返回类型,否则就是class的一个构造器。构造器名要与class名一致。把test2改为Rectangle,或者把Rectangle改为test2 public Rectangle(int w,int h)protected int GetArea()public void drawRect()看看你的三个方法的声明有什么不一样的吗?出错的那个和其他比少了点东西 1、文件名必须和你这个文件中包含的某一个类名相同。2、就是我上面所说除了构造函数,其它方法都需要返回类型。3、如果要一个类能独立运行,包含它的java文件必须要用它的名字作为文件名,而它一定要包含如下方法: public static void main(String[] args)再检查一次你的程序符合以上条件没有。如果还是不行,请注明你的错误是在编译提示还是执行时提示,提示内容。 E:\Tomcat\webapps\ROOT\java>javac test2.javatest2.java:44: cannot resolve symbolsymbol : method appendChar (char)location: class java.lang.StringBuffer dirBuf.appendChar(ch); ^test2.java:49: cannot resolve symbolsymbol : method appendChar (char)location: class java.lang.StringBuffer fileBuf.appendChar(ch); ^2 errors class Rectangle{ protected int width,height; protected int area; public Rectangle(int w,int h) { width=w; height=h; area=GetArea(w,h); } protected int GetArea(int w,int h) { int area; area=w*h; return area; } public void drawRect() { for(int i=width;i>0;i--) System.out.print("*"); System.out.println("\n"); for(int i=height-2;i>0;i--) { System.out.print("*"); for(int j=width-2;j>0;j--) System.out.print(' '); System.out.print("\n"); } for(int i=width;i>0;i--) System.out.print("*"); System.out.print("*"); }}class test2{ public static void main(String [] args){ Rectangle x=new Rectangle(10,2); System.out.println("area:"+x.GetArea(10,2)); x.drawRect(); }} 小问题 java中,String数组和byte数组的区别,为什么要转换? 如何用java处理图像的分辨率问题 tapestry5在eclipese里面的文件怎么放的 .tml文件放在哪里,class文件放在那里? Socket编程中InputStream读取数据的问题 jvm运行奇慢(这是太难了) 关于JAVA编程思想一书的几个问题@ 如何将byte[]转换为hex字符串 有应用服务器还要http服务器干什么? 再次请问applet的问题,在线等…… 求俄罗斯方块的设计思路 这个程序的问题出在哪?
{
protected int width,height;
public Rectangle(int w,int h)
{
width=w;
height=h;
}
protected int GetArea()
{
int area;
area=width*height;
return area;
}
public void drawRect()
{
for(int i=width;i>0;i--)
System.out.print("*");
System.out.println("\n");
for(int i=height-2;i>0;i--)
{
System.out.print("*");
for(int j=width-2;j>0;j--)
System.out.print(' ');
System.out.print("\n");
}
for(int i=width;i>0;i--)
System.out.print("*");
System.out.print("*");
}
}
把test2改为Rectangle,或者把Rectangle改为test2
protected int GetArea()
public void drawRect()
看看你的三个方法的声明有什么不一样的吗?出错的那个和其他比少了点东西
2、就是我上面所说除了构造函数,其它方法都需要返回类型。
3、如果要一个类能独立运行,包含它的java文件必须要用它的名字作为文件名,而它一定要包含如下方法:
public static void main(String[] args)再检查一次你的程序符合以上条件没有。
如果还是不行,请注明你的错误是在编译提示还是执行时提示,提示内容。
test2.java:44: cannot resolve symbol
symbol : method appendChar (char)
location: class java.lang.StringBuffer
dirBuf.appendChar(ch);
^
test2.java:49: cannot resolve symbol
symbol : method appendChar (char)
location: class java.lang.StringBuffer
fileBuf.appendChar(ch);
^
2 errors
{
protected int width,height;
protected int area;
public Rectangle(int w,int h)
{
width=w;
height=h;
area=GetArea(w,h);
}
protected int GetArea(int w,int h)
{
int area;
area=w*h;
return area;
}
public void drawRect()
{
for(int i=width;i>0;i--)
System.out.print("*");
System.out.println("\n");
for(int i=height-2;i>0;i--)
{
System.out.print("*");
for(int j=width-2;j>0;j--)
System.out.print(' ');
System.out.print("\n");
}
for(int i=width;i>0;i--)
System.out.print("*");
System.out.print("*");
}
}class test2
{
public static void main(String [] args){
Rectangle x=new Rectangle(10,2);
System.out.println("area:"+x.GetArea(10,2));
x.drawRect();
}
}