栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在循环中使用if语句?-处理

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在循环中使用if语句?-处理

初步建议:学习正确的Java编码约定,学习如何缩进代码,以及命名变量。

对您的代码进行轻微的重写应该可以使可读的修正:

int scoreStartX = 52;int scoreStartY = 40;int scoreBallSize = 40;// scorePosX/Y means the position the score-ball should be drawnscorePosX = scoreStartX;  // scoreStartX/Y = starting position of score balls scorePosY = scoreStartY;for (int i = 0; i < score; i++) {     ellipse(scorePosX , scorePosY , scoreBallSize , scoreBallSize);    // increment the positions, and wrap to next col if over screen width    scorePosX += scoreBallSize ;   if( scorePosX  > screenWidth) { // next score ball position is beyond the screen       scorePosX = scoreStartX;       scorePosY += scoreBallSize;   }}

进一步重构代码以使用诸如Point之类的东西来表示坐标

Point scoreStartPos = new Point(52, 40);int scoreBallSize = 40;Point scorePos = new Point(scoreStartPos );for (int i = 0; i < score; i++) {    drawCircle(scorePos, scoreBallSize); // a little helper method makes your pre easier to read    // increment the positions, and wrap to next col if over screen width    scorePos.translate( +scoreBallSize, 0);   if( scorePos.getX() > screenWidth) { // next score ball position is beyond the screen       scorePos.setLocation(scoreStartPoint.getX(),      scorePos.getY() + scoreBallSize);   }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/410576.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号