第九天 練習

 

1.1 習題

打印出1000以內的“水仙花數”,所謂“水仙花數”是指一個3位數,其各位數字立方和等於該數本身。例如,153是一個“水仙花數”,因爲153=1的三次方+5的三次方+3的三次方)。

1.1.1 打印水仙花

/**

 * 打印1000以內的水仙花數

 * @author stu

 *

 */

class Test01PrintSXH01{

public static void main(String[] args) {

int x,y,z;

for(int m = 100;m < 1000;m++){

x = m/100;

y = m%100/10;

z = m%10;

if(m == x*x*x + y*y*y + z*z*z){

System.out.println(m);

}

}

}

}

//打印水仙花while循環

class Test01PrintSXH02{

public static void main(String[] args) {

int x,y,z;

int m = 100;

while(m < 1000){

x = m/100;

y = m%100/10;

z = m%10;

if (m == x*x*x + y*y*y + z*z*z) {

System.out.println(m);

}

m++;

}

}

}

1.1.2 通過代碼完成兩個整數內容的交換

//通過代碼完成兩個整數內容的交換

class Test0201{

public static void main(String[] args) {

int x = 1;

int y = 2;

System.out.println("交換前:" + x +"--" + y);

x = x + y;

y = x - y;

x = x - y;

System.out.println("交換後:" + x + "--" + y);

}

}

class Test0202{

public static void main(String[] args) {

int x = 1;

int y = 2;

int t = 0;

System.out.println("交換前:" + x +"--" + y);

t = x;

x = y;

y = t;

System.out.println("交換後:" + x + "--" + y);

}

}

1.1.3 給定3個數,求出3個數中的最大值,並打印出最大值

//給定3個數,求出3個數中的最大值,並打印出最大值

//開始我自己寫的

class Test0301{

public static void main(String[] args) {

int x = 4;

int y = 8;

int z = 6;

int t = 0;

if (x > y) {

t = x;

}else{

t = y;

}

if (t < z){

t = z;

}

System.out.println(t);

}

}

//網上的例子

class Test0302{

public static void main(String[] args) {

int x = 4;

int y = 8;

int z = 6;

int max = z;

if (x > z) {

max = x;

}

if (y > z) {

max = y;

}

if (x > y) {

max = x;

}

System.out.println(max);

}

}

1.1.4  判斷某數能不能被3、5、7同時整除

//判斷某數能不能被3、5、7同時整除

class Test04{

public static void main(String[] args) {

int x = 9;

if(x%3 == 0 && x%5 == 0 && x%7 == 0){

System.out.println(x + "能被3、5、7同時整除");

}else{

System.out.println(x + "不能被3、5、7同時整除");

}

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章