Java-02,计算1+3+5+7+......+99的值

public class HelloWorld{
    public static void main(String[] args) {
        long result = 0;
        long f = 1;
        for(int i = 1; f <= 99; i++){
            result += f;
            f = f + 2;
        }
        System.out.printf("result = %d\n",result);
    }
}

计算结果

2500

posted @ 2020-02-21 21:08  润青  阅读(1255)  评论(0编辑  收藏  举报