java第十周作业

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

 

 1 package test;
 2 public class test {
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8 int a[]={2,4,6,5,9,8,7};
 9 px(a);
10 for (int i = 0; i < a.length; i++) {
11     System.out.println(a[i]);
12 }
13     }
14 
15     private static void px(int[] a) {
16         // TODO Auto-generated method stub
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 b=a[j];
21                     a[j]=a[j+1];
22                     a[j+1]=b;
23                 }
24             }
25         }
26     }
27 
28 }

 

 

 

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

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

 

 

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

 1 package test;
 2 
 3 import java.util.Scanner;
 4 
 5 public class test2 {
 6      public static int year() {
 7             System.out.println("输入年份");
 8             Scanner input = new Scanner(System.in);
 9             int x = input.nextInt();
10             if (x % 4 == 0 && x % 100!=0 || x % 400 == 0) {
11                 System.out.println("该年为闰年");
12             }else {
13                 System.out.println("该年为平年");
14             }
15             return x;
16         }
17         public static void main(String[] args) {
18             year();
19         }
20 }

 

 

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

 1 package test;
 2 
 3 import java.util.Random;
 4 import java.util.Scanner;
 5 
 6 public class test3 {
 7 
 8     /**
 9      * @param args
10      */
11     public static void main(String[] args) {
12         mainMenu();
13     }
14      public static void mainMenu() {
15             Scanner input = new Scanner(System.in);
16             System.out.println("欢迎使用本系统");
17             System.out.println("1.登录");
18             System.out.println("2.注册");
19             System.out.println("3.幸运抽奖");
20             System.out.println("4.退出");
21             System.out.println("请选择");
22             int i = input.nextInt();
23             switch (i) {
24             case 1:
25                 login();
26                 break;
27             case 2:
28                 reg();
29                 break;
30             case 3:
31                 lucky();
32 
33             }
34 
35         }
36 
37         public static void lucky() {
38             Scanner input = new Scanner(System.in);
39             System.out.println("输入一个四位数");
40             Random r = new Random();
41             int x = r.nextInt(10);
42             int num = input.nextInt();
43             while (num < 1000 || num > 10000) {
44                 System.out.println("输入有误,重新输入");
45                 num = input.nextInt();
46                 if (num >= 1000 && num < 10000) {
47                     break;
48                 }
49             }
50             int bai = num % 1000 / 100;
51             if (bai == x) {
52                 System.out.println("幸运会员");
53               } else {
54                 System.out.println("不是幸运会员");
55             }
56 
57             returnMain();
58         }
59 
60         public static void returnMain() {
61             Scanner input = new Scanner(System.in);
62             System.out.println("是否返回主菜单?");
63             if (input.next().equalsIgnoreCase("Y"))
64                 mainMenu();
65             else
66                 System.out.println("谢谢使用");
67         }
68 
69         public static void reg() {
70             // TODO Auto-generated method stub
71             Scanner input = new Scanner(System.in);
72             System.out.println("输入要注册的用户名");
73             String uname = input.next();
74             System.out.println("输入注册密码");
75             String upwd = input.next();
76             System.out.println("注册成功");
77             returnMain();
78 
79         }
80 
81         public static void login() {
82             Scanner input = new Scanner(System.in);
83             System.out.println("输入用户名");
84             String uname = input.next();
85             System.out.println("输入密码");
86             String upwd = input.next();
87             if (uname.equals("ls") && upwd.equals("123")) {
88                 System.out.println("ok");
89             } else {
90                 System.out.println("fail");
91          }
92             returnMain();
93     }
94 }

 

posted @ 2021-05-11 20:45  董澳  阅读(50)  评论(1编辑  收藏  举报