2020.10.16日作业
第七题
package tes; import java.util.Scanner; public class Demo1 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.println("请输入一个三位数"); int i=scanner.nextInt(); isSXH(i); } public static void isSXH(int num){ int a=num%10,b=num/10%10,c=num/100; if ((a*a*a)+(b*b*b)+(c*c*c)==num) { System.out.println(num+"为水仙花数"); }else { System.out.println(num+"不是水仙花数"); } } }
第八题
package tes; import java.util.Scanner; public class Demo2 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.println("请输入第一个整数"); int a =scanner.nextInt(); System.out.println("请输入第二个整数"); int b =scanner.nextInt(); System.out.println("请输入您要进行的运算(0:表示加法运算,1:表示减法运算,2:表示乘法运算,3:表示除法运算)"); int c =scanner.nextInt(); switch (c){ case 0: System.out.println(a+"+"+b+"="+(a+b)); break; case 1: System.out.println(a+"-"+b+"="+(a-b)); break; case 2: System.out.println(a+"*"+b+"="+(a*b)); break; case 3: System.out.println(a+"/"+b+"="+(a/b)); break; } } }
第九题
package tes; import java.util.Random; public class Demo3 { public static void main(String[] args) { int arr[]=new int[5]; int arr1[]=new int[5]; Random random=new Random(); for (int i = 0; i <arr.length ; i++) { arr[i]=random.nextInt((50)+1)*2; arr1[i]=random.nextInt((50)+1)*2; } hebin(arr,arr1); } public static void hebin(int[]arr,int []arr1){ int a[]=new int[arr.length+arr1.length]; for (int i = 0; i <arr.length ; i++) { a[i]=arr[i]; } for (int i = 0; i <arr1.length ; i++) { a[arr.length+i]=arr[i]; } for(int i:a ){ System.out.print(i+","); } } }
第十题
package tes; import java.util.ArrayList; import java.util.Scanner; public class Demo4 { public static void main(String[] args) { ArrayList<Integer>list=new ArrayList<>(); Scanner scanner=new Scanner(System.in); for (int i = 0; i <5 ; i++) { int s =scanner.nextInt(); list.add(s); } int sum=0; for(int i:list ){ if(i>10){ System.out.print("大于10"+i); sum+=i; } } System.out.println("总和"+sum); } }
第十一题
package tes; import java.util.ArrayList; import java.util.Random; public class Demo5 { public static void main(String[] args) { Random random=new Random(); ArrayList<Integer>list=new ArrayList<>(); for (int i = 0; i <10 ; i++) { list.add(random.nextInt(100)); } ArrayList<Integer>list1 =list(list); for (int i:list1 ){ System.out.print(i+","); } } public static ArrayList<Integer>list(ArrayList<Integer>list){ ArrayList<Integer>list1=new ArrayList<>(); for (int i:list ){ if (i<50){ list1.add(i); } } return list1; } }
第十二题
package tes; import java.util.ArrayList; public class Demo6 { public static void main(String[] args) { ArrayList<Integer> list=new ArrayList<Integer>(); list.add(11); list.add(22); list.add(55); list.add(66); list.add(77); list.add(88); for (int i = 0; i <list.size() ; i++) { if (list.get(i)>60){ list.remove(list.get(i)); i--; } } System.out.println(list); } }
第十三题
package tes; import java.util.Random; import java.util.Scanner; public class Demo7 { public static void main(String[] args) { int arr[]=new int[5]; Random random=new Random(); for (int i = 0; i <arr.length ; i++) { arr[i]=random.nextInt(100)+1; } System.out.print("随机生成的数组元素为:"); for(int i:arr ){ System.out.print(i+""); } System.out.print("请输入2-5之间的一个数字"); Scanner scanner=new Scanner(System.in); int num= scanner.nextInt(); System.out.print("符合条件的元素为:"); for(int i:arr ){ if (i%num == 0) { System.out.print(i+""); } } } }