1. 案例:减肥计划
if 版本代码:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//键盘录入一个星期数,用一个变量接收
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个星期数:");
int week = sc.nextInt();
if (week < 1 || week > 7) {
System.out.println("你输入的星期数有误");
} else if (week == 1) {
System.out.println("跑步");
} else if (week == 2) {
System.out.println("游泳");
} else if (week == 3) {
System.out.println("慢走");
} else if (week == 4) {
System.out.println("动感单车");
} else if (week == 5) {
System.out.println("拳击");
} else if (week == 6) {
System.out.println("爬山");
} else {
System.out.println("好好吃一顿");
}
}
}
switch 版本代码:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//键盘录入一个星期数,用一个变量接收
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个星期数:");
int week = sc.nextInt();
switch (week) {
case 1:
System.out.println("跑步");
break;
case 2:
System.out.println("游泳");
break;
case 3:
System.out.println("慢走");
break;
case 4:
System.out.println("动感单车");
break;
case 5:
System.out.println("拳击");
break;
case 6:
System.out.println("爬山");
break;
case 7:
System.out.println("好好吃一顿");
break;
default:
System.out.println("你输入的星期数有误");
}
}
}
2. 逢七过
public class Test {
public static void main(String[] args) {
for (int x = 1; x <= 100; x++) {
if (x % 10 == 7 || x / 10 % 10 == 7 || x % 7 == 0){
System.out.println(x);
}
}
}
}
3. 不死神兔
public class Test {
public static void main(String[] args) {
int[] arr = new int[20];
arr[0] = 1;
arr[1] = 1;
for (int x = 2; x < arr.length; x++) {
arr[x] = arr[x - 2] + arr[x - 1];
}
System.out.println("第二十个月兔子的对数是:" + arr[19]);
}
}
4. 百钱百鸡
public class Test {
public static void main(String[] args) {
for (int x = 0; x <= 20; x++) {
for (int y = 0; y <= 33; y++) {
for (int z = 0; z <= 100; z++) {
if ((x + y + z == 100) && (5 * x + 3 * y + z / 3 == 100) && (z % 3 == 0)) {
System.out.println(x + "," + y + "," + z);
}
}
}
}
}
}
5. 数组元素求和
public class Test {
public static void main(String[] args) {
int[] arr = {68, 27, 95, 88, 171, 996, 51, 210};
int sum = 0;
for (int x = 0; x < arr.length; x++) {
if (arr[x] % 10 != 7 && arr[x] / 10 % 10 != 7 && arr[x] % 2 == 0) {
sum += arr[x];
}
}
System.out.println("sum:" + sum);
}
}
6.数组内容相同
public class Test {
public static void main(String[] args) {
int[] arr = {11, 22, 33, 44, 55};
int[] arr2 = {11, 22, 33, 44, 55};
boolean flag = compare(arr,arr2);
System.out.println(flag);
}
public static boolean compare(int[] arr, int[] arr2) {
if (arr.length != arr2.length) {
return false;
}
for (int x = 0; x < arr.length; x++) {
if (arr[x]!=arr2[x]){
return false;
}
}
return true;
}
}
7. 查找
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//数组静态初始化
int[] arr = {19, 28, 37, 46, 50};
//键盘录入数据
System.out.println("请输入查找值:");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
//定义一个索引,初始值为-1
int index = -1;
//遍历数组
for (int x = 0; x < arr.length; x++) {
if (number == arr[x]) {
index = x;
break;
}
}
//输出索引变量
System.out.println("index:" + index);
}
}
8. 反转
public class Test {
public static void main(String[] args) {
//定义数组,静态初始化
int[] arr = {19, 28, 37, 46, 50};
//循环遍历数组
for (int start = 0, end = arr.length - 1; start <= end; start++, end--) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}