Java第十周作业
1.编写一个方法,实现冒泡排序(由小到大),并调用该方法
import java.util.Scanner; public class Homework1 { public static void paixu() { Scanner input=new Scanner(System.in); System.out.println("输入五个数"); int x[]=new int [5]; for(int i=0;i<x.length;i++){ x[i]=input.nextInt(); } for(int i=0;i<x.length-1;i++){ for(int j=0;j<x.length-1-i;j++){ if(x[j]>x[j+1]){ int temp=x[j]; x[j]=x[j+1]; x[j+1]=temp; } } } System.out.println("排序后为"); for(int i=0;i<x.length;i++){ System.out.print(x[i]+" "); } } public static void main(String[] args){ paixu(); } }
2.编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。 [必做题]
import java.util.Scanner; public class Homework2 { public static void jiechen() { System.out.println("请输入一个数"); jc(); } public static void jc() { int sum = 1; Scanner input = new Scanner(System.in); int a = input.nextInt(); for (int b = 1; b <= a; b++) { sum= sum * b; } System.out.println("这个数的阶乘为" + sum); } public static void main(String[] args) { jiechen(); } }
3.编写一个方法,判断该年份是平年还是闰年。[必做题]
import java.util.Scanner; public class Homework3 { public static void year() { Scanner input = new Scanner(System.in); System.out.println("输入年份"); int year = input.nextInt(); if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { System.out.println("该年是闰年"); } else { System.out.println("该年是平年"); } } public static void main(String[] args) { year(); } }
4.课堂没完成的menu菜单,实现幸运抽奖功能
import java.util.Random; import java.util.Scanner; public class Homework4 { public static void mainMenu() { 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 a=input.nextInt(); switch(a){ case 1: login(); break; case 2: reg(); break; case 3: choujian(); break; } } public static void choujian(){ //输入四位会员卡号,如果百位数等于随机数,幸运会员。否则不是。同时也要询问是否返回主菜单 Scanner input=new Scanner(System.in); System.out.println("输入一个四位数"); Random r=new Random(); int x=r.nextInt(10); int num =input.nextInt(); while (num<100||num>10000){ System.out.println("输入有误,重新输入"); num=input.nextInt(); if(num >= 1000 && num < 10000){ break; } } int bai =num%1000/100; if(bai==x){ System.out.println("幸运会员"); }else{ System.out.println("不是幸运会员"); } returnMain(); } public static void returnMain(){ Scanner input=new Scanner(System.in); System.out.println("是否返回主菜单?Y/N"); if(input.next().equalsIgnoreCase("Y")) mainMenu(); 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("zs")&& upwd.equals("666")){ System.out.println("ok"); }else{ System.out.println("fail"); } returnMain(); } public static void main(String[] args){ mainMenu(); } }