第七次Java作业
1、给定一个有9个整数(1,6,2,3,9,4,5,7,8)的数组,先排序,然后输出排序后的数组的值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package aaa; import java.util.Arrays; public class aaaaa { public static void main(String[] args) { // TODO Auto-generated method stub int a[]= { 1 , 6 , 2 , 3 , 9 , 4 , 5 , 7 , 8 }; Arrays.sort(a); for ( int i:a) { System.out.println(i); } } } |
2、 输出一个double型二维数组(长度分别为5、4,值自己设定)的值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package aaa; public class aaaaa { public static void main(String[] args) { // TODO Auto-generated method stub double a[][]= { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 }, { 9 , 10 , 11 , 12 }, { 13 , 14 , 15 , 16 }, { 17 , 18 , 19 , 20 } }; for ( int i= 0 ;i<a.length;i++) { for ( int j= 0 ;j<a[i].length;j++) { System.out.print(a[i][j] + "" ); } System.out.println(); } } } |
3、 在一个有8个整数(18,25,7,36,13,2,89,63)的数组中找出其中最大的数及其下标。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package aaa; public class aaaaa3 { public static void main(String[] args) { // TODO Auto-generated method stub int a[]= { 18 , 25 , 7 , 36 , 13 , 2 , 89 , 63 }; int max= 0 ,index= 0 ; for ( int i= 0 ;i<a.length;i++) { if (a[i]>max) { max=a[i]; index=i; } } System.out.println( "最大数是" +max+ "下标是" +index); } }<br> |
4、将一个数组中的元素逆序存放
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package aaa; import java.util.Scanner; public class aaaaa4 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input= new Scanner(System.in); int []a= new int [ 5 ]; for ( int i= 0 ;i<a.length;i++) { a[i]=input.nextInt(); } int temp; for ( int i= 0 ;i<a.length/ 2 ;i++) { temp=a[i]; a[i]=a[a.length- 1 ]; a[a.length-i- 1 ]=temp; } for ( int i= 0 ;i<a.length;i++) { System.out.println(a[i]+ " " ); } } } |
5. 将一个数组中的重复元素保留一个其他的清零。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package aaa; import java.util.Arrays; public class aaaaa5 { public static void main(String[] args) { // TODO Auto-generated method stub int a[]= { 12 , 32 , 45 , 36 , 54 , 56 , 76 }; for ( int i= 0 ;i<a.length;i++) { for ( int j= 0 ;j<a.length;j++) { if (a[i]==a[j] && i !=j) a[j]= 0 ; } } for ( int i:a) { System.out.println(i); } } } |
6、给定一维数组{ -10,2,3,246,-100,0,5},计算出数组中的平均值、最大值、最小值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[] = { - 10 , 2 , 3 , 246 , - 100 , 0 , 5 }; int avg = 0 , max = 0 , min = 0 ; int sum = 0 ; for ( int i = 0 ; i < a.length; i++) { sum += a[i]; avg = sum / 7 ; if (a[i] > max) { max = a[i]; } if (a[i] < min) { min = a[i]; } } System.out.println( "平均数" + avg + "最大" + max + "最小" + min); } } |
7、使用数组存放裴波那契数列的前20项 ,并输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int [] x = new int [ 20 ]; x[ 0 ] = 1 ; x[ 1 ] = 1 ; for ( int i = 2 ; i < x.length; i++) { x[i] = x[i - 1 ] + x[i - 2 ]; } for ( int i = 0 ; i < 8 ; i++) { System.out.print(x[i] + " " ); } } } |
8、生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,再输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.Arrays; import java.util.Random; public class test { public static void main(String[] args) { Random r = new Random(); int x[] = new int [ 10 ]; for ( int i = 0 ; i < x.length; i++) { x[i] = r.nextInt( 101 ); } for ( int i = 0 ; i < x.length; i++) { System.out.print(x[i] + " " ); } System.out.println( "排序后:" ); Arrays.sort(x); for ( int i = 0 ; i < x.length; i++) { System.out.print(x[i] + " " ); } } } |
9、做一个菜单切换程序。主菜单1.登陆 2.注册 3幸运抽奖 4 退出。每个菜单可以返回主菜单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | import java.util.Random; import java.util.Scanner; public class test { public static void mainMenu() { Scanner input = new Scanner(System.in); System.out.println( "欢迎使用本系统" ); System.out.println( "1.登录" ); System.out.println( "2.注册" ); System.out.println( "3.幸运抽奖" ); System.out.println( "4.退出" ); System.out.println( "请选择" ); int x = input.nextInt(); switch (x) { case 1 : login(); break ; case 2 : register(); break ; case 3 : luckdraw(); break ; } } public static void luckdraw() { Scanner input = new Scanner(System.in); System.out.println( "输入四位会员卡号" ); Random r = new Random(); int y = r.nextInt( 10 ); int num = input.nextInt(); while (num < 1000 || num > 10000 ) { System.out.println( "输入有误,重新输入" ); num = input.nextInt(); if (num >= 1000 && num < 10000 ) { break ; } } int b = num % 1000 / 100 ; if (b == y) { System.out.println( "幸运会员" ); } else { System.out.println( "不是幸运会员" ); } returnMain(); } public static void returnMain() { Scanner input = new Scanner(System.in); System.out.println( "是否返回主菜单?" ); if (input.next().equalsIgnoreCase( "Y" )) mainMenu(); else System.out.println( "谢谢使用" ); } public static void register() { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println( "输入要注册的用户名" ); String uname = input.next(); System.out.println( "输入注册密码" ); String pwd = input.next(); System.out.println( "注册成功" ); returnMain(); } public static void login() { Scanner input = new Scanner(System.in); System.out.println( "输入用户名" ); String uname = input.next(); System.out.println( "输入密码" ); String pwd = input.next(); if (uname.equals( "admin" ) && pwd.equals( "admin" )) { System.out.println( "成功" ); } else { System.out.println( "失败" ); } returnMain(); } public static void main(String[] args) { mainMenu(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」