Java代码:
package fortest;
//1.打印1~100之间所有奇数的和
//2.打印1~100之间所有是7的倍数的整数的个数及总和(体会设置计数器的思想)
//3.输出所有的水仙花数,所谓水仙花数是指一个3位数,其各个位上数字立方和等于其本身。
//例如:153=1*1*1+3*3*3+5*5*5
public class ForTest3 {
//第一题
static class ForTest31{
public static void main(String[] args) {
int all = 0;
for(int i = 1; i <= 100 ; i++){
if((i % 2 - 1) == 0){
all += i;
}
}
System.out.println(all);
}
}
//第二题
static class ForTest32{
public static void main(String[] args) {
int count =0, num = 0;
for(int i = 1;i <= 100 ;i ++){
if(i % 7 ==0 ){
num ++;
count += i;
}
}
System.out.println("个数 "+ num);
System.out.println("他们的和 "+ count);
}
}
//第三题:水仙花数
static class ForTest33{
public static void main(String[] args) {
int a , b , c ;
for(int i = 100 ;i <= 999; i++){
a = i / 100; //百位
b = i/10%10; //十位
c = i%10; //各位
if(i == a*a*a + b*b*b + c*c*c ){
System.out.println(i);
}
}
}
}
}