day1
从键盘输入一个整数。判断这个数据是否为水仙花数
思路:通过整除和取余分别得到个十百位
static void water() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个整数:");
int w = scanner.nextInt();
// 个位
int ge = w % 10;
// 十位
int shi = (w / 10) % 10;
// 百位
int bai = w / 100;
double result = Math.pow(ge, 3) + Math.pow(shi, 3) + Math.pow(bai, 3);
if ((int) result == w) {
System.out.println("满足条件:"+w+"是水仙花数!!!");
} else {
System.out.println("不满足条件:"+w+"不是水仙花数!!!");
}
}
运行结果如下:
获取三个整数并排序
采用冒泡排序
public static void main(String[] args) {
// 有三个数,x,y,z, 然后按从小打到排序
// 获取三个整数
int x = 0,y=0,z=0;
Scanner input = new Scanner(System.in);
// 冒泡排
int temp = 0;
for (int i = 0; i <a.length-1; i++) {
for (int j=0; j < a.length-1; j++) {
if (a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
// 输出
for (int i = 0; i <3; i++) {
System.out.print(a[i]+"\t");
}
}
运行结果如下:
809*x = 800*x + 9*x,x代表两位数,8*x是两位数,9*x是三位数,计算出x是多少。
思路:通过for循环遍历两位数数值区间,逐一计算条件值,再通过字符串length();方法求出条件值长度,最后进行判断
for (int i = 10; i <99; i++) {
int two = ((8*i)+"").length();
int three = ((9*i)+"").length();
if(two == 2 && three == 3) {
System.out.println("满足"+"\'809*x = 800*x + 9*x\'的数是:\t"+i);
}
}
运行结果如下: