贾云鹏

java第十周作业

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

package lianxi;

public class lianxi1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int []x={10,15,1,6,9,8,74,52,4,23};
        maopao(x);
        for(int i=0;i<x.length;i++){
            System.out.println(x[i]);
        }
    }
    
       public static void maopao(int []x){
           int a=0;
           for(int i=x.length-1;i>0;i--){
               for(int j=0;j<i;j++){
                   if(x[j]>x[j+1]){
                   a=x[j+1];
                   x[j+1]=x[j];
                   x[j]=a;
               }
           }
       }
     }
}

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

package lianxi;
import java.util.Scanner;
public class lianxi2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner s=new Scanner(System.in);
        System.out.println("求阶乘");
        int x=s.nextInt();
        System.out.println(x+"的阶乘是"+jiecheng(x));
       }
        public static int jiecheng(int a){
            int b=1;
            for(int i=a;i>=1;i--){
                 b=b*i;
            }
            return b;
        }

}

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

package lianxi;
import java.util.Scanner;
public class lianxi3 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner s=new Scanner(System.in);
        System.out.println("请输入年份:");
        int y=s.nextInt();
        if(year(y)){
            System.out.println("该年份是闰年");
        }
        else{
            System.out.println("该年份是平年");
        }
        
    }
    public static boolean year(int year){
        if(year%4==0&&year%100!=0||year%400==0){
            return true;
        }
        else{
            return false;
        }
    }
    

}

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

package lianxi;
import java.util.Scanner;
import java.util.Random;
public class lianxi4 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner s=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=s.nextInt();
        while(true){
            switch(1){
            case 1:
                login();
                break;
            case 2:
                register();
                break;
            case 3:
                choujiang();
                break;
            case 4:
                exit();
                break;
            }
            menu();
            int x=s.nextInt();
            i=x;
        }
    }
    public static void menu(){
        Scanner s=new Scanner(System.in);
        System.out.println("请选择菜单");
    }
    public static void login(){
        Scanner s=new Scanner(System.in);
        System.out.println("请输入用户名");
        int a=s.nextInt();
        System.out.println("请输入密码");
        String b=s.next();
        if(123==a&&"abc".equals(b)){
            System.out.println("登录成功");
        }
        else{
            System.out.println("登录失败");
        }
    }
    public static void register(){
        Scanner s=new Scanner(System.in);
        System.out.print("注册账号:") ;
        long a= s.nextLong();
        System.out.print("输入密码:");
        String b=s.next();
        System.out.println("注册成功");
    }
    public static void choujiang(){
        Scanner s=new Scanner(System.in);
        Random r=new Random(11);
        int x=r.nextInt();
        System.out.println("请输入幸运数字");
        int a=s.nextInt();
        if(a==x){
            System.out.println("恭喜你,中奖了。");
        }
        else{
            System.out.println("很遗憾,没中奖。");
        }
    }
    public static void exit(){
        System.exit(0);
    }
}

posted on 2021-05-12 13:04  贾云鹏  阅读(51)  评论(0编辑  收藏  举报

导航