1.1编写程序求 1+3+5+7+……+99 的和值。
public class Demo01 {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i < 100; i++) {
if (i%2==1) {
sum += i;
}
}
System.out.println("累加和的值 " + sum);
}
}
1.2输出所有的花数,所谓花数是指一个数3位数,其每位数字立方和等于其本身,如153 = 111 + 333 + 555
public class Demo02 {
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
int bai = i/100%10;
int shi = i/10%10;
int ge = i%10;
if (i == bai*bai*bai + shi*shi*shi + ge*ge*ge) {
System.out.println(i);
}
}
}
}
2.1定义打印数组元素方法,按照给定的格式打印[11, 33, 44, 22, 55]
//打印数组
public static void printArray(int[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.println(arr[i]+"]");
} else {
System.out.print(arr[i]+", ");
}
}
}
2.2数组元素逆序
public static void receive(int[] arr){
for (int start = 0, end = arr.length-1; start < end; start++,end--) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}