For语句
public class numl {
public static void main(String[] args) {
int x, y, z, i, sum;
for(i=100;i<1000;i++)
z=i%100%10;
y=i/10;
y=y%10;
x=i/ 100;
sum=x*x*x+y*y*y+z*Z*z;
if (sum=i)
System. out. println (sum+"是水仙花数”) ;
}
}
Whi le语句
public class numl {
public static void main(String[] args) {
int x, y, z, i=100, sum;
while(i<1000)
z=i%100%10;
y=i/10;
y=y%10;
x=i/ 100;
sum=x*x*x+y*y*y+z*z*z;
if(sum==i)
System. out. println(sum+”是水仙花数"); .
i++;
}
}
Do-whil e语句
public class numl {
public static void main(String[] args) {
int x, y, z, i=100, sum;
do{
z=i%100%10;
y=i/10;
y=y%10;
x=i/ 100;
sum= Fx*x*x+y*y*y+z*z*z;
if (sum=i)
System. out. println(sum+”是水仙花数”);
} while(i<1000) ;
}
扩展资料
do-while循环的与for循环,while循环的区别
一、循环结构的表达式不同
do-while循环结构表达式为:do{循环体;}。
for循环的结构表达式为:for(单次表达式;条件表达式;末尾循环体){中间循环体}。
while循环的结构表达式为:while(表达式){循环体}。
二、执行时判断方式不同
do-while循环将先运行一次,因为经过第一次do循环后,当检查条件表达式的值时,其值为 不成立时而会退出循环。保证了至少执行do{ }内的语句一次。
for循环执行的中间循环体可以为一个语句,也可以为多个语句,当中间循环体只有一个语句时,其大括号{}可以省略,执行完中间循环体后接着执行末尾循环体。
while循环执行时当满足条件时进入循环,进入循环后,当条件不满足时,执行完循环体内全部语句后再跳出(而不是立即跳出循环)。
C语言程序:
#include "stdio.h"运行结果:
使用while循环找出的水仙花数