我目前正在從一本教科書中編寫一個項目,並且在while循環中出現了一個模糊的問題。教材中的代碼如下:Java - Alternate While循環聲明
while(getImage().getWidth(applet) <= 0);
double x = applet.getSize().width/2 - width()/2;
double y = applet.getSize().height/2 - height()/2;
at = AffineTransform.getTranslateInstance(x, y);
我從來沒有見過在末尾用分號表示的while循環,所以我將我的代碼解釋爲標準;
while(getImage().getWidth(applet) <= 0) {
double x = applet.getSize().width/2 - width()/2;
double y = applet.getSize().height/2 - height()/2;
at = AffineTransform.getTranslateInstance(x, y);
}
有人能解釋一下兩者的區別嗎?我將我的代碼更改爲第一個,我的程序運行良好,但我不明白區別。
像Mike F.說,它可能會阻止,直到發生某些事件,但如果在教科書中,它可能會被設計爲阻止你,看看你是否抓住它並像你一樣糾正。這裏有一篇關於雅的文章:http://users.csc.calpoly.edu/~jdalbey/SWE/JavaPitfalls.html – 2012-07-15 14:59:28