第十次Java作业
Posted yujiafu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第十次Java作业相关的知识,希望对你有一定的参考价值。
1.编写一个方法,实现冒泡排序(由小到大),并调用该方法
public class h1{ public static void paixu(int[] b) { for (int i = 0; i < b.length - 1; i++) { for (int j = 0; j < b.length - 1 - i; j++) { if (b[j] < b[j + 1]) { int m = 0; m=b[j]; b[j] = b[j + 1]; b[j + 1] = m; } } } } public static void main(String[] args) { // TODO Auto-generated method stub int x[] = { 5, 4, 3, 1 }; paixu(x); for (int i : x) { System.out.println(i); } } }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
public class h2 { public static void jc(int y) { int b = 1; for (int i = 1; i <= y; i++) { b *= i; } System.out.println(b); } public static void main(String[] args) { int x = 5; jc(x); } }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
import java.util.Scanner; public class h3{ /** * @param args */ public static void pd(int year) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { System.out.println(year + "是闰年"); } else { System.out.println(year + "是平年"); } } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("输入年份"); pd(input.nextInt()); } }
4.课堂没完成的menu菜单,实现幸运抽奖功能
import java.util.Random; import java.util.Scanner; public class h4 { public static void SY() { Scanner input = new Scanner(System.in); System.out.println("<<<<<<<<<<欢迎使用会员抽奖系统>>>>>>>>>>"); System.out.println(" 1.登录"); System.out.println(" 2.注册"); System.out.println(" 3.幸运抽奖"); System.out.println(" 4.退出"); System.out.println(" 请输入数字编号进行操作"); int i = input.nextInt(); switch (i) { case 1: login(); break; case 2: reg(); break; case 3: lucky(); default: System.out.println("输入错误,请重新输入!"); SY(); } } private static void lucky() { Scanner input = new Scanner(System.in); Random r = new Random(); int luck = r.nextInt(10); System.out.println("请输入四位会员卡号"); int id = input.nextInt(); if (luck == id) { System.out.println("恭喜您是幸运会员"); } else { System.out.println("对不起您不是幸运会员"); } returnMain(); } public static void returnMain() { Scanner input = new Scanner(System.in); System.out.println("是否返回主菜单?(Y/S)"); if (input.next().equalsIgnoreCase("Y")) SY(); else System.out.println("谢谢使用"); } public static void reg() { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("输入要注册的用户名"); String uname = input.next(); System.out.println("输入注册密码"); String upwd = input.next(); System.out.println("注册成功"); returnMain(); } public static void login() { Scanner input = new Scanner(System.in); System.out.println("输入用户名"); String uname = input.next(); System.out.println("输入密码"); String upwd = input.next(); if (uname.equals("123") && upwd.equals("123")) { System.out.println("输入正确"); } else { System.out.println("输入错误"); } returnMain(); } public static void main(String[] args) { SY(); } }
以上是关于第十次Java作业的主要内容,如果未能解决你的问题,请参考以下文章