首页 > 代码库 > Java试题

Java试题

1、不使用循环,等比数列输出整型 n、2n、4n、8n……当大于max时,反向输出8n、4n、2n、n。

例如 n=10,max=100。 输出: 

10

20

40

80

80

40

20

10

 

解题思路: 递归实现。

public class Test {

    public static void main(String[] args) {
        multiply(10, 100);
    }
    public static void multiply(int n, int max) {
        if (n < max) {
            System.out.println(n);
            multiply(2 * n, max);
            System.out.println(n);
        }
    }
}

如果需要输出大于100后的数,则代码如下:

public class Test {

    public static void main(String[] args) {

        multiply(10, 100,10);

    }
    
    public static void multiply(int n, int max,int min) {
        if (n < max) {
            System.out.println(n);
            multiply(2 * n, max, min);
        } else{
            System.out.println(n);
            divide(n,min);
        }
    }
    
    public static void divide(int n, int min) {
        if (n >= min) {
            System.out.println(n);
            divide(n / 2, min);
        }
    }
}

 

 

2、Comparator 排序。 对某些对象 collection 进行整体排序。排序时将此接口的实现传递给Collections.sort方法或者Arrays.sort方法排序。实现 o1.compareTo(o2) 方法。如果是升序排序:o1小于o2,返回-1(负数),相等返回0,o1大于o2,返回1(正数);反之降序。

代码如下:

public class Supermarket{
    
    private String name;
    private double distance; //距离
    private boolean isBusiness; // 是否营业
    private double money; // 营业额

    public Supermarket(String name, double distance, boolean isBusiness,
            double money) {
        super();
        this.name = name;
        this.distance = distance;
        this.isBusiness = isBusiness;
        this.money = money;
    }

    @Override
    public String toString() {
        return name + "\t" + distance + "\t" + (isBusiness ? "是" : "否") + "\t"
                + money + "\r\n";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getDistance() {
        return distance;
    }

    public void setDistance(double distance) {
        this.distance = distance;
    }

    public boolean isBusiness() {
        return isBusiness;
    }

    public void setBusiness(boolean isBusiness) {
        this.isBusiness = isBusiness;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

}
public class Menu {

    public static void main(String[] args) {

        List<Supermarket> list = new ArrayList<Supermarket>();

        Supermarket s1 = new Supermarket("天天超市", 1.0, true, 5000);
        Supermarket s2 = new Supermarket("地地超市", 3.5, false, 2000);
        Supermarket s3 = new Supermarket("人人超市", 2.5, true, 6000);
        Supermarket s4 = new Supermarket("神神超市", 5.0, false, 1000);
        Supermarket s5 = new Supermarket("佛佛超市", 2.0, true, 3000);

        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);
        
//        System.out.println(list);
        String choose ="";
        Scanner input = new Scanner(System.in);
        while(!"0".equals(choose)){
            System.out.println("1、店名\t2、距离\t3、是否营业\t4、营业额\t0、退出");
            System.out.println("请选择:");
            choose = input.next();
            switch (choose) {
            case "1":
                Collections.sort(list,new Comparator<Supermarket>() {
                    @Override
                    public int compare(Supermarket o1, Supermarket o2) {
                        return o1.getName().compareTo(o2.getName());
                    }
                });
                for (Supermarket supermarket : list) {
                    System.out.print(supermarket.toString());
                }
                break;
            case "2":
                Collections.sort(list, new Comparator<Supermarket>() {

                    @Override
                    public int compare(Supermarket o1, Supermarket o2) {
                        if(o1.getDistance()<o2.getDistance())
                            return -1;
                        return 1;
                    }
                });
                for (Supermarket supermarket : list) {
                    System.out.print(supermarket.toString());
                }
                break;
            case "3":
                Collections.sort(list, new Comparator<Supermarket>() {

                    @Override
                    public int compare(Supermarket o1, Supermarket o2) {
                        if(o1.isBusiness())
                            return -1;
                        return 1;
                    }
                });
                for (Supermarket supermarket : list) {
                    System.out.print(supermarket.toString());
                }
                break;
            case "4":
                Collections.sort(list, new Comparator<Supermarket>() {

                    @Override
                    public int compare(Supermarket o1, Supermarket o2) {
                        if(o1.getMoney()<o2.getMoney())
                            return -1;
                        return 1;
                    }
                });
                for (Supermarket supermarket : list) {
                    System.out.print(supermarket.toString());
                }
                break;
            case "0":
                System.out.println("程序结束。");
                break;
            default:
                System.out.println("你输入的有误,请重新输入!");
                break;
            }
        }
        input.close();
    }
    
}

 

3、SimpleDateFormat 时间转换工具类。用来格式化时间日期。

示例:显示当前时间,格式为 格式为 2017715日 上午94223秒, 每5秒显示一次时间。 

解题思路:创建线程,格式化日期。 代码如下:

public class Timer extends Thread {

    @Override
    public void run() {

        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 a HH时mm分ss秒");    // a --> AM, PM
        try {
            while (true) {
                Date date = new Date();    //获取当前时间
                String time = format.format(date); //格式化时间
                System.out.println(time);    
                Thread.sleep(5000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

 

Java试题