求和
int sum = 0;
for (int i = 1; i <= 10 ; i++){
sum = sum + i;
}System.out.println(sum);
求偶数和
int sum = 0;
for (int i = 1; i <= 10 ; i++){
if (i % 2 == 0){
sum = sum + i;
}
}System.out.println(sum);
求奇数和
int sum = 0;
for (int i = 1; i <= 10 ; i++){
if (i % 2 == 1){
sum = sum + i;
}
}System.out.println(sum);
打印水仙花数
for (int i = 100; i <= 999 ; i++){
int x = i % 10;
int y = i % 100 / 10;
int z = i / 100;
if (i =x*x*x + y*y*y +z*z*z){
System.out.println(i + "是水仙花数");
}
}
统计水仙花数
int a = 0;
for (int i = 100; i <= 999 ; i++){
int x = i % 10;
int y = i % 100 / 10;
int z = i / 100;
if (i =x*x*x + y*y*y +z*z*z){
a++;
}
}System.out.println("一共有" + a +"个水仙花数");
九九乘法表
for (int i=1; i<10; i++) {
for (int k=10-i; k<9; k++) {
System.out.print("\t");
}
for (int j=i; j<10;j++ ) {
System.out.print(i+"*"+j+"="+i*j+"\t");
}System.out.println();
}
2:while循环的格式?要能看懂执行流程
初始化语句;
while (判断条件语句){
循环体语句;
控制条件;
}
用while循环完成如下案例
求和
int a = 1;
int sum = 0;
while (a <= 10){
sum = sum + a;
a++;
}System.out.println(sum);
纸张折叠成珠穆朗玛峰高度的次数
class print99 {
public static void main(String[] args)
{
int a = 1; //高度
int b = 0; //计数器
while (a <= 8848000){
a = 2*a;
b++;
}System.out.println(b-1);
}
}