日期:2014-05-20  浏览次数:20748 次

一JAVA新手例子,帮看下
程序如下:
class Triangle 
{
double area;
int height, length;

public static void main(String[] args)
{

Triangle[] ta = new Triangle[4];
int x = 0;

while(x < 4)
{
ta[x] = new Triangle();
ta[x].height = (x + 1) * 2;
ta[x].length = x + 4;
ta[x].setArea();
System.out.print("triangle " + x + ", area");
System.out.println(" = " + ta[x].area);
x = x + 1;

}
int y = x;
x = 27;
Triangle t5 = ta[2];
ta[2].area = 343;
System.out.print("y = " + y);
System.out.println(", ta area = " + t5.area);

}

void setArea()
{
area=(height * length) / 2;
}
}

不太懂为什么最后 Y=4,程序中的X=27值起迷惑作用?
还有,我用的是eclipse,怎样才能断点跟单步调试,谢谢!

------解决方案--------------------
y的赋值不是在x改变值之前么,那就是x执行完循环的值4啊。
断点在run菜单里面,先debug,然后菜单里面就有步入和步出的选项了,对应的快捷键后面也有
------解决方案--------------------
能从 while(x<4) 退出来,说明x=4
y=x也是4,
x=27完全没用。