第十周第九次
编写一个方法,实现冒泡排序(由小到大),并调用该方法
package dijiucizuoye; 2 3 import java.util.Scanner; 4 5 public class work { 6 public static void px(int []a) { 7 for (int i = 0; i < a.length; i++) { 8 for(int j=0;j<a.length;j++){ 9 if (a[i]<a[j]) { 10 int temp=a[i]; 11 a[i]=a[j]; 12 a[j]=temp; 13 } 14 } 15 16 } 17 } 18 public static void main(String[] args){ 19 Scanner input=new Scanner(System.in); 20 int []a=new int [5]; 21 for(int i=0;i<a.length;i++){ 22 a[i]= input.nextInt(); 23 } 24 px(a); 25 for (int i = 0; i <a.length ; i++) { 26 System.out.print(a[i]+" "); 27 } 28 } 29 }
编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5
package dijiucizuye;
2
3 import java.util.Scanner;
4
5 public class Sclass {
6 public static int jc(int a) {
7 int sum=1;
8 for (int i = 1; i <=a ; i++) {
9 sum*=i;
10 }
11 return sum;
12 }
13
14 public static void main(String[] args){
15 Scanner input=new Scanner(System.in);
16 int n=input.nextInt();
17 System.out.println(jc(n));
18 }
19 }
编写一个方法,判断该年份是平年还是闰年
package dijiucizuoye;
2
3 import java.util.Scanner;
4
5 public class A {
6 public static void pd(int year){
7 if (year%4==0&&year%100!=0||year%400==0) {
8 System.out.println("闰年");
9 }
10 else {
11 System.out.println("平年");
12 }
13 }
14
15
16 public static void main(String[] args) {
17 Scanner input=new Scanner(System.in);
18 pd(input.nextInt());
19 }
20 }
package dijiucizuoye;
2
3 import java.util.Random;
4 import java.util.Scanner;
5
6 public class work4 {
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 reg();
22 break;
23 case 3:
24 lucky();
25 break;
26 }
27
28
29 }
30
31 public static void returnMain(){
32 Scanner input=new Scanner(System.in);
33 System.out.println("是否返回主菜单?");
34 if(input.next().equalsIgnoreCase("Y"))
35 mainMenu();
36 else
37 System.out.println("谢谢使用");
38 }
39
40 public static void reg() {
41 // TODO Auto-generated method stub
42 Scanner input=new Scanner(System.in);
43 System.out.println("输入要注册的用户名");
44 String uname=input.next();
45 System.out.println("输入注册密码");
46 String upwd=input.next();
47 System.out.println("注册成功");
48
49 returnMain();
50
51 }
52
53
54 public static void login(){
55 Scanner input=new Scanner(System.in);
56 System.out.println("输入用户名");
57 String uname=input.next();
58 System.out.println("输入密码");
59 String upwd=input.next();
60 if(uname.equals("zs")&&upwd.equals("123")){
61 System.out.println("ok");
62 }else{
63 System.out.println("fail");
64 }
65 returnMain();
66 }
67 private static void lucky() {
68 // 输入四位会员卡号,如果百位数等于随机数,幸运会员。否则不是。同时也要询问是否返回主菜单
69 Scanner input=new Scanner(System.in);
70 Random r=new Random();
71 int luck=r.nextInt(10);
72 System.out.println("幸运数字为"+luck);
73 System.out.println("输入四位数会员卡号");
74 int id= input.nextInt();
75 if (id/100%10==luck) {
76 System.out.println("幸运会员是"+id+"号");
77 }
78 else {
79 System.out.println("不是幸运会员");
80 }
81 returnMain();
82 }
83
84
85 public static void main(String[] args) {
86 mainMenu();
87
88 }
89
90
91 }