求1+2+3……+1000的和,把和输出,计算每步结果中有多少个最后以8结尾的

public class HM0503 {
    public static void main(String[] args){
        int s=0;//声明一个变量
        int a=0;
        for(int i=1;i<=1000;i++){
            s=s+i;
            if(s%10==8){
                a++;
            }
        }
        System.out.println("1000以内总合="+s+"    多少个以8结尾="+a);
    }
}

1000以内总合=500500    多少个以8结尾=100

posted @ 2016-07-10 11:05  树上有个猴  阅读(713)  评论(0)    收藏  举报