本文共 1407 字,大约阅读时间需要 4 分钟。
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+"个");}
}
“转载地址:http://oftli.baihongyu.com/