Java第10次作业

1.编写一个方法,实现冒泡排序(由小到大),并调用该方法

 1 package work10;
 2 
 3 public class test1 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         int []x={1,3,2,5,8,7,9};
11         mp(x);
12         for (int i = 0; i < x.length; i++) {
13             System.out.println(x[i]);
14         }
15     }
16     public static void mp(int[]a){
17         for (int i = 0; i < a.length - 1; i++) {
18             for (int j = 0; j < a.length - 1 - i; j++) {
19                 if (a[j] > a[j + 1]) {
20                     int temp = a[j];
21                     a[j] = a[j + 1];
22                     a[j + 1] = temp;
23                 }
24             }
25         }
26     }
27 }

 2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]

 1 package work10;
 2 
 3 public class Test2 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         int sum=jc(5);
11         System.out.println(sum);
12     }
13     public static int jc(int n){
14         int sum=1;
15         for (int i =1; i <=n; i++) {
16             sum*=i;
17         }
18         return sum;
19     }
20     
21  }

3.编写一个方法,判断该年份是平年还是闰年。[必做题]

 1 package mianxiang;
 2 
 3 import java.util.Scanner;
 4 
 5 //3.编写一个方法,判断该年份是平年还是闰年。[必做题]
 6 public class HomeWork3 {
 7     public static void panduan(int year) {
 8         if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
 9             System.out.println("闰年!");
10         } else {
11             System.out.println("非闰年!");
12         }
13     }
14 
15     public static void main(String[] args) {
16         Scanner input = new Scanner(System.in);
17         System.out.println("请输入一个年份:");
18         int a = input.nextInt();
19         panduan(a);
20     }
21 
22 }

4.课堂没完成的menu菜单,实现幸运抽奖功能

 1 package hm;
 2 
 3 import java.util.Random;
 4 import java.util.Scanner;
 5 
 6 public class hm4 {
 7     public static void mainMenu() {
 8         Scanner input = new Scanner(System.in);
 9         System.out.println("欢迎使用本系统");
10         System.out.println("1.登录");
11         System.out.println("2.注册");
12         System.out.println("3.幸运抽奖");
13         System.out.println("4.退出系统");
14         System.out.println("请选择:");
15         int i = input.nextInt();
16         switch (i) {
17         case 1:
18             login();
19             break;
20         case 2:
21             register();
22             break;
23         case 3:
24             xycj();
25             break;
26         }
27     }
28 
29     public static void register() {
30         Scanner input = new Scanner(System.in);
31         System.out.println("输入用户名");
32         String uname = input.next();
33         System.out.println("输入密码");
34         String pwd = input.next();
35         System.out.println("注册成功");
36         returnMain();
37 
38     }
39 
40     public static void returnMain() {
41         Scanner input = new Scanner(System.in);
42         System.out.println("是否返回主菜单?Y/N");
43         if (input.next().equalsIgnoreCase("Y"))
44             mainMenu();
45         else
46             System.out.println("谢谢使用");
47     }
48 
49     public static void login() {
50         Scanner input = new Scanner(System.in);
51         System.out.println("输入用户名");
52         String uname = input.next();
53         System.out.println("输入密码");
54         String pwd = input.next();
55         if (uname.equals("admin") && pwd.equals("admin"))
56             System.out.println("成功");
57         else
58             System.out.println("失败");
59         returnMain();
60 
61     }
62     
63     public static void xycj() {
64         Scanner input = new Scanner(System.in);
65         System.out.println("输入一个4位会员卡号");
66         int number = input.nextInt();
67         int baiwei = number / 100 % 10;
68         Random r = new Random();
69         int a = (int) r.nextInt(10);
70         if (baiwei == a) {
71             System.out.println("该用户是幸运会员");
72         } else {
73             System.out.println("该用户不是幸运会员");
74         }
75         returnMain();
76     }
77     
78     public static void main(String[] args) {
79         mainMenu();
80     }
81 }

 

posted @ 2021-05-11 17:24  唐一南  阅读(55)  评论(0)    收藏  举报