Java第九次作业
练习:
求平方和
1 package boke5月7日; 2 3 public class M1 { 4 public static void square(double a, int b) { 5 int sum = (int) (a * a + b * b); 6 System.out.println("输出平方和"); 7 System.out.println(sum); 8 9 } 10 11 public static void main(String[] args) { 12 square(1.5, 3); 13 } 14 }
作业
1.编写一个方法,实现冒泡排序(由小到大),并调用该方法
1 package boke5月7日; 2 3 import java.util.Scanner; 4 5 public class Method { 6 public static void Sort() { 7 Scanner input = new Scanner(System.in); 8 System.out.println("输入五个数"); 9 int[] a = new int[5]; 10 for (int i = 0; i < a.length; i++) { 11 a[i] = input.nextInt(); 12 } 13 for (int i = 0; i < a.length - 1; i++) { 14 for (int j = 0; j < a.length - i - 1; j++) { 15 if (a[j] > a[j + 1]) { 16 int temp = a[j]; 17 a[j] = a[j + 1]; 18 a[j + 1] = temp; 19 } 20 } 21 } 22 System.out.println("排序后:"); 23 for (int i1 : a) { 24 System.out.println(i1); 25 } 26 } 27 28 public static void main(String[] args) { 29 Sort(); 30 } 31 }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
1 package boke5月7日; 2 3 public class Method1 { 4 public static int jc(int n) { 5 int sum = 1; 6 for (int i = 1; i <= n; i++) { 7 sum *= i; 8 } 9 System.out.println("阶乘:"); 10 return sum; 11 } 12 13 public static void main(String[] args) { 14 System.out.println(jc(5)); 15 } 16 }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
1 package boke5月7日; 2 3 import java.util.Scanner; 4 5 public class Method2 { 6 public static void nian() { 7 Scanner input = new Scanner(System.in); 8 System.out.println("输入一个年份"); 9 int year = input.nextInt(); 10 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { 11 System.out.println("闰年"); 12 } else { 13 System.out.println("平年"); 14 } 15 } 16 17 public static void main(String[] args) { 18 nian(); 19 } 20 }
4.课堂没完成的menu菜单,实现幸运抽奖功能
1 package boke5月7日; 2 3 import java.util.Random; 4 import java.util.Scanner; 5 6 public class Menu { 7 8 public static void mainMenu() { 9 Scanner input = new Scanner(System.in); 10 System.out.println("欢迎使用本系统"); 11 System.out.println("1.登录"); 12 System.out.println("2.注册"); 13 System.out.println("3.幸运抽奖"); 14 System.out.println("4.退出"); 15 System.out.println("请选择"); 16 int i = input.nextInt(); 17 switch (i) { 18 case 1: 19 login(); 20 break; 21 case 2: 22 reg(); 23 break; 24 case 3: 25 lucky(); 26 27 } 28 29 } 30 31 private static void lucky() { 32 // 输入四位会员卡号,如果百位数等于随机数,幸运会员。否则不是。同时也要询问是否返回主菜单 33 Scanner input = new Scanner(System.in); 34 System.out.println("输入四位会员卡号"); 35 int card = input.nextInt(); 36 Random r = new Random(); 37 int a = r.nextInt(); 38 if (card / 100 % 10 == a) { 39 System.out.println("幸运会员"); 40 } else { 41 System.out.println("不是幸运会员"); 42 } 43 System.out.println("是否返回主菜单?"); 44 } 45 46 public static void returnMain() { 47 Scanner input = new Scanner(System.in); 48 System.out.println("是否返回主菜单?"); 49 if (input.next().equalsIgnoreCase("Y")) 50 mainMenu(); 51 else 52 System.out.println("谢谢使用"); 53 } 54 55 public static void reg() { 56 // TODO Auto-generated method stub 57 Scanner input = new Scanner(System.in); 58 System.out.println("输入要注册的用户名"); 59 String uname = input.next(); 60 System.out.println(uname); 61 System.out.println("输入注册密码"); 62 String upwd = input.next(); 63 System.out.println(upwd); 64 System.out.println("注册成功"); 65 returnMain(); 66 67 } 68 69 public static void login() { 70 Scanner input = new Scanner(System.in); 71 System.out.println("输入用户名"); 72 String uname = input.next(); 73 System.out.println("输入密码"); 74 String upwd = input.next(); 75 if (uname.equals("zs") && upwd.equals("123")) { 76 System.out.println("ok"); 77 } else { 78 System.out.println("fail"); 79 } 80 returnMain(); 81 } 82 83 public static void main(String[] args) { 84 mainMenu(); 85 86 } 87 88 }