重新认识Java(3_1)————流程控制语句(水仙花数)

计算水仙花数就是 每位数字的立方相加等于他本身

public class Text04 {
	public static void main(String[] args) {
		int x = 100;
		while ( x <1000) {
			int a , b, c ;
			a = (x / 100);
			b = (x / 10 % 10);
			c = (x % 10);
			if(x == a*a*a + b*b*b + c*c*c){
				System.out.print("while方法:"+x+",");
			}
			x++;
		}
		System.out.println();
		
		int z = 100;
		do{
			int a , b, c ;
			a = (z / 100);
			b = (z / 10 % 10);
			c = (z % 10);
			if(z == a*a*a + b*b*b + c*c*c){
				System.out.print("do-while方法:"+z+",");
			}
			z++;
		}while ( z <1000);
		
		System.out.println();
		
		for(int y = 100 ; y <= 999 ; y++ ){
			int a, b, c;
			a = (y / 100);
			b = (y / 10 % 10);
			c = (y % 10);
			if(y == a*a*a + b*b*b + c*c*c){
				System.out.print("for方法"+y+",");
			}
		}
		
	}
}

猜你喜欢

转载自blog.csdn.net/m0_37729047/article/details/81253527
今日推荐