範例:求1-100的奇數和及1-100的隅數和
- class java_ex24_continue_break {
- public static void main(String[] args) {
- //求1-100的奇數和
- int sum = 0;
- for (int i=1;i<=100 ; i++)
- {
- if(i % 2 == 0){
- continue;
- }
- sum+=i;
- }
- System.out.println("1-100的奇數和 = " + sum);
- //求1-100的隅數和
- int sum1 = 0;
- int j = 0 ;
- while(true){
- j++;
- if(j % 2 != 0){
- continue;
- }
- sum1+=j;
- if (j>=100)
- {
- break;
- }
- }
- System.out.println("1-100的隅數和 = " + sum1);
- }
- }
執行結果: