求和
比如求1~10所有数的和
int sum = 0;
for (int x = 1;x<=10 ;x++ ) {
sum+=x;
}
System.out.println(sum);
求偶数和
int sum = 0;
for (int x=1;x<=10 ;x++ ) {
if (x%2==0) {
sum+=x;
}else{
continue;
}
}
System.out.println(sum);
求奇数和
int sum = 0;
for (int x=1;x<=10 ;x++ ) {
if (x%2==1) {
sum+=x;
}else{
continue;
}
}
System.out.println(sum);
打印水仙花数\ 合一块儿做了
统计水仙花数/
方法一:
int count = 0;
for (int x = 100;x<1000 ;x++ ) {
if ((x%10)*(x%10)*(x%10)+(x/10%10)*(x/10%10)*(x/10%10)+(x/100)*(x/100)*(x/100)==x) {
System.out.println(x);
count+=1;
}
}
System.out.println(count);
方法二:
int count = 0;
for (int x = 100;x<1000 ;x++ ) {
int g = x/1%10;
int s = x/10%10;
int b = x/100%10;
if (g*g*g+s*s*s+b*b*b=x) {
System.out.println(x);
count++;
}
}
System.out.println(count);
九九乘法表:利用循环嵌套(外层控制行数,内层控制列数)
for (int i = 1;i<=9 ;i++ ) {
for (int j = 1;j<=i ;j++ ) {
System.out.print(j+"*"+i+"= "+i*j+"\t");
}
System.out.println();//换行
}