5.7 第十周java作业

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

 1 import java.util.Scanner;
 2 
 3 public class zuoye {
 4      public static void mppx(int []x) {
 5          for (int i = 0; i < x.length; i++) {
 6              for(int j=0;j<x.length;j++){
 7               if (x[i]<x[j]) {
 8                int temp=x[i];
 9                x[i]=x[j];
10                 x[j]=temp;
11                }
12            }
13 
14        }
15     }
16      public static void main(String[] args){
17         Scanner input=new Scanner(System.in);
18         int []a=new int [5];
19          for(int i=0;i<a.length;i++){
20              a[i]= input.nextInt();
21         }
22          mppx(a);
23          for (int i = 0; i <a.length ; i++) {
24              System.out.print(a[i]+" ");
25         }
26     }
27 
28 }

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

 1 import java.util.Scanner;
 2 public class zuoye {
 3     public static int jc(int x) {
 4         int sum=1;
 5        for (int i = 1; i <=x ; i++) {
 6           sum*=i;
 7        }
 8        return sum;
 9     }
10 
11    public static void main(String[] args){
12         Scanner input=new Scanner(System.in);
13        int n=input.nextInt();
14        System.out.println(n+"的阶层为"+jc(n));
15     }
16 }

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

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

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

 1 import java.util.Random;
 2 import java.util.Scanner;
 3 public class zuoye {
 4     public static void mainMenu(){
 5         Scanner input=new Scanner(System.in);
 6         System.out.println("欢迎使用本系统");
 7         System.out.println("1.登录");
 8         System.out.println("2.注册");
 9         System.out.println("3.幸运抽奖");
10         System.out.println("4.退出");
11         System.out.println("请选择");
12         int i=input.nextInt();
13         switch(i){
14         case 1:
15             login();
16             break;
17         case 2:
18             reg();
19             break;
20         case 3:
21             lucky();
22         
23         }
24         
25         
26     }
27     
28     private static void lucky() {
29         // 输入四位会员卡号,如果百位数等于随机数,幸运会员。否则不是。同时也要询问是否返回主菜单
30         Scanner input=new Scanner(System.in);
31         Random r=new Random();
32         int luck=r.nextInt(10);
33      System.out.println("输入四位数会员卡号");
34        int id= input.nextInt();
35         if (id/100%10==luck) {
36            System.out.println("幸运会员是"+id+"号");
37       }
38        else {
39            System.out.println("不是幸运会员");
40       }
41        returnMain();
42 
43     }
44 
45     public static void returnMain(){
46         Scanner input=new Scanner(System.in);
47         System.out.println("是否返回主菜单?");
48         if(input.next().equalsIgnoreCase("Y"))
49             mainMenu();
50         else
51             System.out.println("谢谢使用");
52     }
53     
54     public static void reg() {
55         // TODO Auto-generated method stub
56         Scanner input=new Scanner(System.in);
57         System.out.println("输入要注册的用户名");
58         String uname=input.next();
59         System.out.println("输入注册密码");
60         String upwd=input.next();
61         System.out.println("注册成功");
62         returnMain();
63         
64         
65     }
66 
67 
68     public static void login(){
69         Scanner input=new Scanner(System.in);
70         System.out.println("输入用户名");
71         String uname=input.next();
72         System.out.println("输入密码");
73         String upwd=input.next();
74         if(uname.equals("zs")&&upwd.equals("123")){
75             System.out.println("ok");
76         }else{
77             System.out.println("fail");
78         }
79         returnMain();        
80     }
81     
82 
83     
84     public static void main(String[] args) {
85         mainMenu();
86 
87     }
88 }

 

posted @ 2021-05-12 15:31  辛事成  阅读(50)  评论(0编辑  收藏  举报