请大家帮我看看这道题,看似没错但是却出错.class Rectangle{protected int width,height;public Rectangle(int w,int h){width=w;height=h;}public void drawRect(){for(int i=width;i>0;i--)System.out.print("#");System.out.println();for(int i=

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/01 05:40:41
请大家帮我看看这道题,看似没错但是却出错.class Rectangle{protected int width,height;public Rectangle(int w,int h){width=w;height=h;}public void drawRect(){for(int i=width;i>0;i--)System.out.print(请大家帮我看看这道题,看似没错但是却出错.
class Rectangle
{
protected int width,height;
public Rectangle(int w,int h)
{
width=w;
height=h;
}
public void drawRect()
{
for(int i=width;i>0;i--)
System.out.print("#");
System.out.println();
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("\n");
}
}
public class MakeRectangle {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int w=Integer.valueOf(args[0]).intValue();
int h=Integer.valueOf(args[0]).intValue();
Rectangle rect=new Rectangle(w,h);
rect.drawRect();
}
}

请大家帮我看看这道题,看似没错但是却出错.class Rectangle{protected int width,height;public Rectangle(int w,int h){width=w;height=h;}public void drawRect(){for(int i=width;i>0;i--)System.out.print("#");System.out.println();for(int i=
这样运行就可以
javac MakeRectangle.java
java MakeRectangle 5
你用的是命令行参数,在运行的时候,必须给定参数
需要注意的,是不管你给什么类型的,它都认为是String类型的(main方法的参数是String[]),在应用中
你可以显式的转换成想要的类型
另外在你的程序中出现两次for循环没有{}的情况
不放{} for循环默认把它下边的第一句代码作为循环体,
这也是可以的,但是不规范,也不利于其他人阅读,应该避免这样使用
这样运行就可以呀,效果是
#####
# #
# #
# #
#####