2013-06-30 63 views
-1
public class whatever { 
    public static void main(String[] arguments){ 
     int points = 0; 
     int target = 100; 
     tagetLoop: 
     while (target <= 100) { 
      for (int i = 0; i < target; i++) { 
       if (points > 50) 
        break tagetLoop; 
       points = points + i; 
       System.out.println("Points: " + points); 

      } 
     } 

    } 
} 

每當我運行這個,最終的輸出將是「分數:55」。爲什麼是55而不是50?在Loop中循環,爲什麼輸出這個數字?

+1

它應該是如果(points> = 50) – jospratik

+3

你知道什麼是調試器嗎? – 1ac0

+0

歡迎來到堆棧溢出!調試是所有程序員的關鍵技能。我強烈建議你學習如何使用你的IDE內置調試器。您還可以將更多的System.out.println()語句添加到代碼中,以查看發生了什麼。 –

回答

3

points值將是

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55 

它將與值50不會退出,因爲它永遠不會等於50

4

因爲0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55