Java基础00-基础知识练习12
1. 减肥计划
1.1 if语句实现
import java.util.Scanner; public class Demo01 { public static void main(String[] args) { //键盘录入一个星期数,用一个变量接收 Scanner sc = new Scanner(System.in); System.out.println("请输入一个星期数:"); int week = sc.nextInt(); //对星期数进行判断,这里用 if 语句实现 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("好好吃一顿"); } } }
输出:
1.2 switch语句实现
import java.util.Scanner; public class Demo02 { public static void main(String[] args) { //键盘录入一个星期数,用一个变量接收 Scanner sc = new Scanner(System.in); System.out.println("请输入一个星期数:"); int week = sc.nextInt(); //对星期数进行判断,这里用 switch 语句实现 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.逢7过
public class Demo03 { public static void main(String[] args) { for (int i = 1; i <= 100; i++) { if ((i % 7 == 0) || (i / 10 == 7) || (i % 10 == 7)) { System.out.println(i); } } } }
输出:
3. 不死神兔
public class Demo04 { public static void main(String[] args) { int[] arr = new int[20]; arr[0] = 1; arr[1] = 1; // 1 1 2 3 5 8 for (int i = 2; i < arr.length; i++) { arr[i] = arr[i - 2] + arr[i - 1]; } //输出数组中最后一个元素的值,就是第20个月的兔子对数 System.out.println("第二十个月兔子的对数是:" + arr[19]); } }
输出:
4. 百钱百鸡
public class Demo05 { public static void main(String[] args) { int gong,mu,xiao; // 第1层循环,用于表示鸡翁的范围 for(gong=0;gong<=20;gong++) { // 第2层循环,用于表示鸡母的范围 for(mu=0;mu<=33;mu++) { // 用于表示鸡雏的变量 xiao=100-gong-mu; if (xiao%3==0&&gong*5+mu*3+xiao/3==100) { System.out.println("公鸡"+gong+",母鸡"+mu+",小鸡"+xiao); } } } } }
输出:
5. 数组元素求和
public class Demo06 { public static void main(String[] args) { int arr[]={68,27,95,88,171,996,51,210}; int sum=0; for (int i = 0; i < arr.length; i++) { if (arr[i]%2==0&&arr[i]%10!=7&&arr[i]/10%10!=7) { sum=sum+arr[i]; } } System.out.println(sum); } }
输出:
6. 数组内容相同
import java.util.Iterator; public class Demo07 { public static boolean getCom(int arr1[],int arr2[]) { // 首先比较数组长度,如果长度不相同,数组内容肯定不相同,返回false if (arr1.length!=arr2.length) { return false; } // 其次遍历,比较两个数组中的每一个元素,只要有元素不相同,返回false for (int i = 0; i < arr1.length; i++) { for (int j = 0; j < arr2.length; j++) { if (arr1[i]!=arr2[j]) { return false; } } } // 最后循环遍历结束后,返回true return true; } public static void main(String[] args) { int arr1[]= {10,20,30}; int arr2[]= {10,20,30,40}; boolean re=getCom(arr1,arr2); System.out.println(re); } }
输出:
7. 查找
import java.util.Scanner; public class Demo08 { public static int getCha(int arr[], int num) { int index=-1; for (int i = 0; i < arr.length; i++) { if (arr[i] == num) { index=1; return index; } } return index; } public static void main(String[] args) { int arr[] = { 19, 28, 37, 46, 50 }; Scanner scanner = new Scanner(System.in); System.out.println("录入一个数据"); int num = scanner.nextInt(); int index = getCha(arr, num); System.out.println(index); } }
输出:
8. 反转
public class Demo09 { public static void getFan(int arr[]) { int temp; // 循环遍历数组,这一次初始化语句定义两个索引变量,判断条件是开始索引小于等于结束索引 for (int start = 0, end = arr.length - 1; start <= end; start++, end--) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; } } public static void getShu(int arr[]) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + ","); } } public static void main(String[] args) { int arr[] = { 19, 28, 37, 46, 50 }; System.out.print("原数组:"); getShu(arr); System.out.print("\n反转后数组:"); getFan(arr); getShu(arr); } }
输出:
9. 评委打分
public class Demo10 { private static double getMin(double[] arr) { double min = arr[0]; for (int i = 1; i < arr.length; i++) { if (min > arr[i]) { min = arr[i]; } } return min; } private static double getMax(double[] arr) { double max = arr[0]; for (int i = 1; i < arr.length; i++) { if (max < arr[i]) { max = arr[i]; } } return max; } private static double getSum(double[] arr) { double sum = 0; for (int i = 0; i < arr.length; i++) { sum = sum + arr[i]; } return sum; } public static void main(String[] args) { double arr[] = new double[6]; Scanner scanner = new Scanner(System.in); System.out.println("请输入6个评委为参赛的选手打分"); for (int i = 0; i < 6; i++) { System.out.println("第"+(i+1)+"位评委"); arr[i] = scanner.nextDouble(); } System.out.println("去掉一个最高分:"); double max = getMax(arr); System.out.println(max); System.out.println("去掉一个最低分:"); double min=getMin(arr); System.out.println(min); System.out.println("求平均分"); double sum=getSum(arr); double avg=(sum-max-min)/(arr.length-2); System.out.println(avg); } }
输出: