Java第六次作业

1.给定一个有9个整数(162394578)的数组,先排序,然后输出排序后的数组的值。

 1 package bbb;
 2 
 3 import java.util.Arrays;
 4 
 5 public class test {
 6 
 7     public static void main(String[] args) {
 8 
 9         // TODO Auto-generated method stub
10 
11         int a[]=new int[] {1,6,2,3,9,4,5,7,8};
12 
13         Arrays.sort(a);
14 
15         for(int d:a) {
16 
17            System.out.println(d);
18 
19         }
20 
21     }
22 
23 }

 

 

2.输出一个double型二维数组(长度分别为54,值自己设定)的值。

package bbb;

public class test {

 

    public static void main(String[] args) {

   

        double a[][]={ {1,9,8,7},{2,4,6,8},{1,3,5,7},{1,2,3,4},{4,5,6,7}};

        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个整数(18257361328963)的数组中找出其中最大的数及其下标。

package bbb;

 

public class test {

 

    public static void main(String[] args) {

   

        int a[]=new int[] {18,25,7,36,13,2,89,63};

        int max=0;

        int index=0;

        for(int i=0;i<a.length;i++) {

           if(a[i]>max) {

               max=a[i];

               index=i;

           }

        }

        System.out.println("最大数为:"+max+"    "+"下标为:"+index);

    }

 

}

 

 

4.将一个数组中的元素逆序存放。

 1 package bbb;
 2 
 3  
 4 
 5 public class test {
 6 
 7  
 8 
 9     public static void main(String[] args) {
10 
11  
12 
13 int a[]=new int[] {1,2,3,4,5,6};
14 
15         for(int i=a.length-1;i>=0;i--)
16 
17         System.out.println(a[i]);
18 
19     }
20 
21  
22 
23 }

 

 

5.将一个数组中的重复元素保留一个其他的清零。

 1 package bbb;
 2 
 3  
 4 
 5 public class test {
 6 
 7  
 8 
 9     public static void main(String[] args) {
10 
11  
12 
13 int a[]=new int[] {23,45,67,87,45,99};
14 
15         for(int i=0;i<a.length;i++) {
16 
17            for(int k=i+1;k<a.length;k++) {
18 
19                if(a[i]==a[k])
20 
21                    a[k]=0;
22 
23            }
24 
25         }
26 
27         for(int d:a)
28 
29         System.out.println(d);
30 
31     }

 

 

 

6.给定一维数组{-1023246-10005},计算出数组中的平均值,最大值,最小值。

 1 package bbb;
 2 
 3 public class test {
 4 
 5     public static void main(String[] args) {
 6 
 7  
 8 
 9 int a[]=new int[] {-10,2,3,246,-100,0,5};
10 
11         double avg=0;
12 
13         int max=0;
14 
15         int min=0;
16 
17         int sum=0;
18 
19         for(int i=0;i<a.length;i++) {
20 
21            if(a[i]>max) {
22 
23                max=a[i];
24 
25            }
26 
27            if(a[i]<min) {
28 
29                min=a[i];
30 
31            }
32 
33            sum+=a[i];
34 
35            avg=sum/a.length;
36 
37         }
38 
39        
40 
41         System.out.println("平均值"+avg+"最大值"+max+"最小值"+min);
42 
43     }
44 
45  
46 
47 }

 

 

7.使数组存放斐波那契数列的前20项,并输出。

 1 package bbb;
 2 
 3 public class test {
 4 
 5     public static void main(String[] args) {
 6 
 7  
 8 
 9 int a[]=new int[20];
10 
11          a[0]=1;
12 
13          a[1]=1;
14 
15         for(int i=2;i<a.length;i++) {
16 
17            a[i]=a[i-2]+a[i-1];
18 
19         }
20 
21         for(int d:a) {
22 
23            System.out.println(d);
24 
25         }
26 
27     } 
28 
29 }

 

 

8.生成一个长度为10的随机整数数组(每个数都是0-100之间),输出,排序后,在输出。

 1 package bbb;
 2 
 3 import java.util.Scanner;
 4 
 5 import java.util.Arrays;
 6 
 7 import java.util.Random;
 8 
 9  
10 
11 public class test {
12 
13  
14 
15     public static void main(String[] args) {
16 
17         // TODO Auto-generated method stub
18 
19 Random r=new Random();
20 
21         int a[]=new int[10];
22 
23         for(int i=0;i<a.length;i++) {
24 
25            a[i]=r.nextInt(101);
26 
27         }
28 
29         for(int d:a) {
30 
31            System.out.println(d);
32 
33         }
34 
35         System.out.println("排序后再输出");
36 
37         Arrays.sort(a);
38 
39         for(int c:a) {
40 
41            System.out.println(c);
42 
43         }
44 
45     }

 

 

}

 

 

9.做一个菜单切换程序,主菜单1.登录2.注册3.幸运抽奖4.退出。每个菜单可以返回主菜单。

  1 package bbb;
  2 
  3 import java.util.Scanner;
  4 
  5 import java.util.Random;
  6 
  7 public class menu {
  8 
  9     public static void showmain() {
 10 
 11         System.out.println("1.登录");
 12 
 13         System.out.println("2.注册");
 14 
 15         System.out.println("3.幸运抽奖");
 16 
 17         System.out.println("4.退出");
 18 
 19         Scanner input=new Scanner(System.in);
 20 
 21         int i=input.nextInt();
 22 
 23         switch (i) {
 24 
 25         case 1:
 26 
 27            login();
 28 
 29            break;
 30 
 31         case 2:
 32 
 33            zhuche();
 34 
 35 break;
 36 
 37         case 3:
 38 
 39            lucky();
 40 
 41            break;
 42 
 43         case 4:
 44 
 45            tuichu();
 46 
 47            break;
 48 
 49         }
 50 
 51     }
 52 
 53     public static void login() {
 54 
 55         Scanner input=new Scanner(System.in);
 56 
 57         System.out.println("输入用户名");
 58 
 59         String name=input.next();
 60 
 61         System.out.println("输入密码");
 62 
 63         String pwd=input.next();
 64 
 65         if(name.equals(pwd)) {
 66 
 67            System.out.println("登陆成功");
 68 
 69         }else {
 70 
 71            System.out.println("登陆失败");
 72 
 73         }
 74 
 75        
 76 
 77         System.out.println("是否返回主菜单?Y/N");
 78 
 79         if(input.next().equalsIgnoreCase("y"))
 80 
 81            showmain();
 82 
 83         else
 84 
 85            System.out.println("谢谢使用");
 86 
 87     }
 88 
 89  
 90 
 91     public static void zhuche() {
 92 
 93         Scanner input=new Scanner(System.in);
 94 
 95         System.out.println("输入用户名");
 96 
 97         String a=input.next();
 98 
 99         System.out.println("输入密码");
100 
101         String b=input.next();
102 
103         if(b.equals(a)) {
104 
105             System.out.println("登陆成功,请返回主菜单");
106 
107             showmain();
108 
109         }else {
110 
111             System.out.println("登陆失败,请重新登录");
112 
113             zhuche();
114 
115         }
116 
117        
118 
119     }
120 
121     public static void lucky() {
122 
123         Random r=new Random();
124 
125         System.out.println("幸运抽奖,请输入一个数(0-100)");
126 
127         Scanner input=new Scanner(System.in);
128 
129         int i=input.nextInt();
130 
131         int q=r.nextInt(101);
132 
133         if(i==q) {
134 
135             System.out.println("恭喜你,中奖了,再抽一次");
136 
137             lucky();
138 
139         }else {
140 
141             System.out.println("很遗憾,未中奖,返回主菜单");
142 
143             showmain();
144 
145         }
146 
147     }
148 
149     public static void tuichu() {
150 
151         System.out.println("谢谢使用");
152 
153     }
154 
155     public static void main(String[] args) {
156 
157         showmain();
158 
159     }
160 
161 }

 

posted @ 2023-05-21 15:53  coldlane  阅读(14)  评论(0编辑  收藏  举报