欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

JavaSE经典编程示例,javase编程示例

来源: javaer 分享于  点击 3668 次 点评:274

JavaSE经典编程示例,javase编程示例


                JavaSE经典编程示例<1>

for循环

class ForDemo4 {
    public static void main(String[] args) {
        //求1-100之和。
        //方式2
        int sum3 = 0;

        for(int x=0; x<=100; x+=2) {
                sum3 += x;
        }

        System.out.println("1-100偶数之和是:"+sum3);
        System.out.println("------------------");
    }
}

水仙花数

class ForDemo8 {
    public static void main(String[] args) {
        int count = 0;      

        for(int x=100; x<1000; x++) {
            //获取每一个三位数的个,十,百的数据
            int ge = x%10;
            int shi = x/10%10;
            int bai = x/10/10%10;

            if(x == (ge*ge*ge+shi*shi*shi+bai*bai*bai)) {
                count++;
            }
        }       
        System.out.println("水仙花数共有"+count+"个");
    }
}

回文数

class ForDemo7 {
    public static void main(String[] args) {
        for(int x=10000; x<100000; x++) {

            int ge = x%10;
            int shi = x/10%10;
            int bai  = x/10/10%10;
            int qian = x/10/10/10%10;
            int wan = x/10/10/10/10%10;

            if((ge==wan) && (shi==qian) && (ge+shi+qian+wan==bai){
                System.out.println(x);
            }
        }
    }
}

For循环特例1:

“`
class ForDemo9 {
public static void main(String[] args) {
int count = 0;

    for(int x=1; x<=1000; x++) {
        if(x%3==2 && x%5==3 && x%7==2) {
            count++;
            System.out.println(x);
        }
    }
    System.out.println("满足这样条件的数据共有:"+count+"个");
}

}

相关文章

    暂无相关文章

用户点评