2015-04-01 39 views
-1

當我嘗試使用自動格式化時,評論無法正常使用。這是代碼之前,我用自動格式化:在同一行上自動格式化評論

if (x > 125 && x < 295 && y > 180 && y < 350) { //if mouse in first tower 

     if (x > bar1.x1 && x < bar1.x2 && y > bar1.y1 && y < bar1.y2){ //if mouse in first bar 
     } 
     if (x > bar2.x1 && x < bar2.x2 && y > bar2.y1 && y < bar2.y2){ //if mouse in second bar 
     } 
     if (x > bar3.x1 && x < bar3.x2 && y > bar3.y1 && y < bar3.y2){ //if mouse in third bar 
     } 
     if (x > bar4.x1 && x < bar4.x2 && y > bar4.y1 && y < bar4.y2){ //if mouse in fourth bar 
     } 
     if (x > bar5.x1 && x < bar5.x2 && y > bar5.y1 && y < bar5.y2){ //if mouse in fifth bar 
     } 

    } 

這是我使用自動格式化後的代碼:

if (x > 125 && x < 295 && y > 180 && y < 350) { // if mouse in first 
                // tower 

     if (x > bar1.x1 && x < bar1.x2 && y > bar1.y1 && y < bar1.y2) { // if 
                     // mouse 
                     // in 
                     // first 
                     // bar 
     } 
     if (x > bar2.x1 && x < bar2.x2 && y > bar2.y1 && y < bar2.y2) { // if 
                     // mouse 
                     // in 
                     // second 
                     // bar 
     } 
     if (x > bar3.x1 && x < bar3.x2 && y > bar3.y1 && y < bar3.y2) { // if 
                     // mouse 
                     // in 
                     // third 
                     // bar 
     } 
     if (x > bar4.x1 && x < bar4.x2 && y > bar4.y1 && y < bar4.y2) { // if 
                     // mouse 
                     // in 
                     // fourth 
                     // bar 
     } 
     if (x > bar5.x1 && x < bar5.x2 && y > bar5.y1 && y < bar5.y2) { // if 
                     // mouse 
                     // in 
                     // fifth 
                     // bar 
     } 

    } 

我該如何解決這個問題?

+0

您使用哪個IDE? – tomse 2015-04-01 19:31:39

回答

1

您是否嘗試將評論放在自己的路線上?

if (x > bar1.x1 && x < bar1.x2 && y > bar1.y1 && y < bar1.y2) { 
    //if mouse in first bar 
    doFirstBarStuff(); 
} 
if (x > bar2.x1 && x < bar2.x2 && y > bar2.y1 && y < bar2.y2) { 
    //if mouse in second bar 
    doSecondBarStuff(); 
} 
+0

我做到了,但我認爲它看起來應該是評論的行。 – nmelssx 2015-04-01 19:38:50

+0

然後在相關的'if(...)'之前放置註釋,也許這看起來會更好。 – rossum 2015-04-01 21:48:24

0

而不是所有的意見考慮使用的方法與表現力的名稱,例如:

boolean mouseInside(Bar bar, int x, int y) { 
    return x > bar.x1 && x < bar.x2 && y > bar.y1 && y < bar.y2; 
} 

那麼你的代碼可以是這樣,也沒有必要的註釋:

if (mosueInside(bar1, x, y)) { 
} 

if (mouseInside(bar2, x, y)) {   
} 
0

其實,如果我不喜歡這樣,它的工作原理:

//if-mouse-in-first-bar