java基础练习题

最近有个朋友想学习java看了一本java开发的书,可是背后的习题不会做就帮他做做,方法也许会有千万种,我只是随便写了一种,反正是能实现就行


1.java打印1~1000范围中的所有水仙花数

public class ShuiXianFlower {

/**
* @desc所谓水仙花数是指一个三位数,其各位数字立方和等于该数本身,例如371=3*3*3 + 7*7*7 + 1*1*1
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 1; i < 1000; i++) {
int a = i / 100; //分解百位
int b = i / 10 % 10;//分解十位
int c = i % 10;//分解个位
if (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3) == i)
System.out.println("水仙花数 : " +i);
}
}

}

java通过代码完成两个整数内容的呼唤

public class ChangeIntegerNum {
/**
* @param 完成两个整数内容的交换
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 3;
int b = 5;
int temp;
temp = a;
a = b;
b = temp;
System.out.println("a ="+a + "b=" +b);
}
}

3.给定3个数字,求出这三个数字中的最大值,并将最大值输出。

public class PrintMaxNum {
/**
* @param 给定三个数字,求出这三个数字中的最大值
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 3,b=5,c=7,max = c;
if(a > b){
max=a;
}
if(a > c){
max=a;
}
if(b>c){
max=b;
}
System.out.println("最大数为:" + max);
}
}

4。判断某数能否被3,5,7同时整除。

public class ZhengChuNum {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("请输入一个数字");
int num = scan.nextInt();
if(num%3==0&&num%5==0&&num%7==0){
System.out.println(num + "能被3,5,7同时整除");
}else{
System.out.println(num + "不能被3,5,7同时整除");
}
}
}



猜你喜欢

转载自blog.csdn.net/weimo1234/article/details/77017564