JAVA笔记二十六:练习题(三)

一、面向过程

public class work {
    //    public int day(int a) {
//        if (a >= 0 && a <= 6) {
//            switch (a) {
//                case 0:
//                    System.out.println("星期日");
//                    break;
//                case 1:
//                    System.out.println("星期一");
//                    break;
//                case 2:
//                    System.out.println("星期二");
//                    break;
//                case 3:
//                    System.out.println("星期日三");
//                    break;
//                case 4:
//                    System.out.println("星期四");
//                    break;
//                case 5:
//                    System.out.println("星期五");
//                    break;
//                case 6:
//                    System.out.println("星期六");
//                    break;
//            }
//        }
//        return a;
//    }


//    public void num(int a, int b, int c) {
//        if (a > b && a > c && b > c) {
//            System.out.println(a + ">" + b + ">" + c);
//        } else if (a > b && a < c) {
//            System.out.println(c + ">" + a + ">" + b);
//        } else if (a < c && b > c) {
//            System.out.println(b + ">" + c + ">" + a);
//        } else if (a > c && b < c){
//            System.out.println(a + ">" + c + ">" + b);
//        }else if (a < b && a < c && b < c){
//            System.out.println(c + ">" + b + ">" + a);
//        }else if (a < b && a > c){
//            System.out.println(b + ">" + a + ">" + c);
//        }
//    }


    //	计算 1 加到 n(n>=2 的整数)的总和
//    public void num(int n) {
//        int b = 0;
//        if (n >= 2) {
//            for (int i = 1; i <=n;i++) {
//                b += i;
//            }
//            System.out.println(b);
//        }
//    }


    // 求一个整数的绝对值
//    public int num(int n){
//        n = n>=0?n:-n;
//        return n;
//    }


    //	判断第一个数字是否是第二个数字的整数倍数
//    public void num(int a,int b){
//        if (a%b==0){
//            System.out.println("true");
//        }else {
//            System.out.println("false");
//        }
//    }


    //在类中编写一个方法,判断数字是否是素数
//    public void num(int a) {
//        boolean f = true;
//        if (a == 1) {
//            f = false;
//        }
//        for (int i = 2; i < a; i++) {
//            if (a % i == 0) {
//                f = false;
//                break;
//            }
//        }
//        System.out.println(f);
//    }


    //请编写一个方法,输入正方形的边长,然后利用星号和空格,打印具有那个边长的一个空心正方形
//    public void num(int a) {
//        for (int i = 0; i < a; i++) {
//            System.out.print("* ");
//        }
//        System.out.println();
//
//        for (int i = 0; i < a - 2; i++) {
//            System.out.print("* ");
//            for (int j = 0; j < a - 2; j++) {
//                System.out.print("  ");
//            }
//            System.out.println("* ");
//        }
//
//        for (int i = 0; i < a; i++) {
//            System.out.print("* ");
//        }
//        System.out.println();
//    }


    //编写一个方法判断是否是“回文”:输入数字,如果是“回文”,返回true,否则返回false
//    public boolean num(int a){
//        boolean f = false;
//        int n = a;//将数字备份
//        String s = "";
//        while (n>0){
//            int y = n%10;
//            n = n/10;
//            s=s+y;
//        }
//        String s2 = Integer.toString(a);
//        if (s2.equals(s)){
//            f = true;
//        }
//        return f;
//    }


    //判断三个边长能否组成三角形
//    public void num(int a, int b, int c) {
//        if (a != 0 && b != 0 && c != 0) {
//            int x = a + b;
//            int y = a + c;
//            int z = b + c;
//            boolean t = x > c && y > b && z > a;
//            System.out.println(t);
//        }
//    }


    //用程序找出每位数的立方和等于该数本身值的所有的 3 位数。(水仙花数)
    public boolean num(int n) {
        if (n < 100 || n > 1000) {
            return false;
        }
        int a = n / 100;//百位
        int c = n % 10;// 个位
        int b = n / 10 % 10;//十位
        return a * a * a + b * b * b + c * c * c == n;
    }
}

一、面向对象

public class work1 {
    public static void main(String[] args) {
        work date = new work();
//        Scanner scan=new Scanner(System.in);
//        System.out.print("请输入您要查询的星期:");
//        int a=scan.nextInt();
//        date.day(a);


//        Scanner scan=new Scanner(System.in);
//        System.out.println("请输入a、b、c:");
//        int a=scan.nextInt(),b=scan.nextInt(),c=scan.nextInt();
//        date.num(a,b,c);

        //	计算 1 加到 n(n>=2 的整数)的总和
//        Scanner scan=new Scanner(System.in);
//        System.out.println("请输入n:");
//        int n = scan.nextInt();
//        date.num(n);


        //求一个整数的绝对值
//        Scanner scan=new Scanner(System.in);
//        System.out.println("请输入n:");
//        int n = scan.nextInt();
//        int date1 = date.num(n);
//        System.out.println(date1);


        //	判断第一个数字是否是第二个数字的整数倍数
//        Scanner scan=new Scanner(System.in);
//        System.out.println("请输入a、b:");
//        int a=scan.nextInt(),b=scan.nextInt();
//        date.num(a,b);


        //在类中编写一个方法,判断数字是否是素数
//        Scanner scan=new Scanner(System.in);
//        System.out.println("请输入a:");
//        int a = scan.nextInt();
//        date.num(a);

//请编写一个方法,输入正方形的边长,然后利用星号和空格,打印具有那个边长的一个空心正方形
//        Scanner scan=new Scanner(System.in);
//        System.out.println("请输入a:");
//        int a = scan.nextInt();
//        date.num(a);

        //编写一个方法判断是否是“回文”:输入数字,如果是“回文”,返回true,否则返回false
//        Scanner scan=new Scanner(System.in);
//        System.out.println("请输入a:");
//        int a = scan.nextInt();
//        boolean date1 = date.num(a);
//        System.out.println(date1);

        //判断三个边长能否组成三角形
//        Scanner scan=new Scanner(System.in);
//        System.out.println("请输入a、b、c:");
//        int a=scan.nextInt(),b=scan.nextInt(),c=scan.nextInt();
//        date.num(a,b,c);


        //用程序找出每位数的立方和等于该数本身值的所有的 3 位数。(水仙花数)
        for (int i=100;1<1000;i++){
            if (date.num(i)){
                System.out.println(i);
            }
        }
    }
}

二、面向过程

public class work2 {
//    斐波那契序列
//    有一个序列,首两项为 0,1,以后各项值为前两项值之和。写一个方法来实现求这个序列的和
//    public void num(int n) {
//        int sum = 0;
//        int x = 0;
//        int y = 1;
//        for (int i = 0; i < n; i++) {
//            System.out.println(x);
//            sum += x;
//            int t = x + y;
//            x = y;
//            y = t;
//        }
//    }


    // 写一个 MyPoint 完全封装类,其中含有私有的 int 类型的 x 和 y 属性,
    // 分别用公有的 getX 和 setX、getY 和 setY方法访问,定义一个 toString 方法用来显示这个对象的 x、y 的值,
    // 如显示(1,2),最后用 main方法测试。
    private int x;
    private int y;

    public void setX(int x) {
        if (x > 0)
            this.x = x;
    }

    public int getX() {
        return x;
    }

    public void setY(int y) {
        if (y > 0)
            this.y = y;
    }

    public int getY() {
        return y;
    }

    public String toString() {
        return "(" + x + "," + y + ")";
    }

    public boolean equals(work2 date){
        if(date.getX()==this.getX()&&date.getY()==this.getY()){
            return true;
        }
        return false;
    }
}

二、面向对象

public class work2_1 {
    public static void main(String[] args) {
        work2 date = new work2();
        //    斐波那契序列
//    有一个序列,首两项为 0,1,以后各项值为前两项值之和。写一个方法来实现求这个序列的和
//
//        date.num(6);


        // 写一个 MyPoint 完全封装类,其中含有私有的 int 类型的 x 和 y 属性,
        // 分别用公有的 getX 和 setX、getY 和 setY方法访问,定义一个 toString 方法用来显示这个对象的 x、y 的值,
        // 如显示(1,2),最后用 main方法测试。
        Scanner scan = new Scanner(System.in);
        int x = scan.nextInt();
        int y = scan.nextInt();
        date.setX(x);
        date.setY(y);
//        System.out.println(date.toString());
        work2 date2 = new work2();
        date2.setY(x);
        date2.setX(y);
        System.out.println(date==date2);
        System.out.println(date2.equals(date));
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五毒幽泉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值