Java练习、每日一题、共100题

  Java每日一题:1
  编写一个 Java 程序, 用 if-else 语句判断某年份是否为闰年。

* 经查阅资料:年分为闰年和平年。
* 闰年的判断方法:
*              公历年份是4的倍数,且不是100的倍数,为普通闰年。
*              公历年份是整百数,且必须是400的倍数才是世纪闰年
* 考虑升级:
*              判断闰年且判断是普通闰年还是世纪闰年。
*              若不是闰年,提示为平年。
* 最终功能:
*              获取用户输入年份判断是普通闰年、世纪闰年还是平年。
*              每次查询完成后询问用户是否继续查询,按1继续查询,按任意键推出查询。


Java每日一题:2
          编写一个 Java 程序在屏幕上输出 1!+2!+3!+……+10!的和。

 *  编写一个 Java 程序在屏幕上输出 1!+2!+3!+……+10!的和。
 *
 *  考虑改进:
 *           给定任意数字n,计算1-n的所有数字阶乘之和:
 *           例如:用户输入5,则计算5!+4!+3!+2!+1!
 *
 *  阶乘:factorial
 *
 *  功能:
 *           计算任意数字n的1-n的所以阶乘之和。
 *           计算结束后可选择是否继续。
package cn.itcast_Java100;

import java.util.Scanner;

public class Java2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("欢迎使用累计阶乘计算器");

        while (true) {
            System.out.println("请输入一个数字n,用于求1-n的所有数字的阶乘的和。");
            int n = sc.nextInt();
            int sum = 0;
            for (int y = 1; y <= n; y++) {
                sum = factorial.factorialcal(y) + sum;
            }
            System.out.println("1-" + n + "的所有数的阶乘的和为:" + sum);
            System.out.println("计算已结束,继续计算请按1,任意键可退出。");
            int quit = sc.nextInt();
            if (quit != 1) {
                break;
            }
        }
    }
}

class factorial {
    //该方法用于求任意数字n的阶乘。
    public static int factorialcal(int n) {
        int sum = 1;
        for (int i = 1; i <= n; i++) {
            sum *= i;
        }
        return sum;
    }
}

Java每日一题:3

(1) 编写一个圆类 Circle

(2) 编写一个圆柱体类 Cylinder, 它继承于上面的 Circle 类。

* 题目:
*       cicle类拥有:
*           一个成员变量:Radius(私有,浮点型)
*           两个构造方法:无参构造将半径设置为0 带参构造用于半径的初始化
*           三个成员方法:获取圆的面积 周长 输出圆的半径 周长 面积到屏幕
*      
*       cylinder类拥有:
            ①一个成员变量double hight(私有, 浮点型); // 圆柱体的高;
            ②构造方法Cylinder (double r, double h )//创建 Circle 对象时将半径初始化为 r
            ③ 成员方法
                    double getVolume( )
                    //获取圆柱体的体积
                    void showVolume( )
                    //将圆柱体的体积输出到屏幕
                    编写应用程序, 创建类的对象, 分别设置圆的半径、 圆柱体的高, 计算并分别显示圆半径、
                    圆面积、 圆周长, 圆柱体的体积。
*
package cn.itcast_Java100;

public class Java3 {
    public static void main(String[] args) {
        Circle c = new Circle(2.5);
        Cylinder cy = new Cylinder(2.5, 5);
        c.showCircle();
        cy.showCylinder();
    }

}

class Circle {
    private double Radius;

    public Circle() {
        this.Radius = 0.0;
    }

    public Circle(double r) {
        this.Radius = r;
    }

    public double getArea() {
        return Math.PI * Radius * Radius;
    }

    public double getPerimeter() {
        return 2 * Math.PI * Radius;
    }

    public void showCircle() {
        System.out.println("圆的半径为:" + Radius + "。");
        System.out.println("圆的周长为:" + getPerimeter() + "。");
        System.out.println("圆的面积为:" + getArea() + "。");
    }
}

class Cylinder extends Circle {
    private double height;

    public Cylinder(double r, double h) {
        super(r);
        this.height = h;
    }

    public double getVolume() {
        return super.getArea() * height;
    }

    public void showCylinder() {
        System.out.println("圆柱体的体积为" + getVolume() + "。");
    }

}

Java每日一题:4

        编写一个Java应用程序﹐从键盘读取用户输入两个字符串,并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。要进行异常处理,对输入的不符合要求的字符串提示给用户,不能使程序崩溃。

public class Exer5 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s1,s2;
        int i1,i2;
        float f1,f2;
        try {
            System.out.println("请输入第一个字符串");
            s1 = new String(scanner.nextLine());
            System.out.println("请输入第二个字符串");
            s2 = new String(scanner.nextLine());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        i1 = Integer.parseInt(s1);
        i2 = Integer.parseInt(s1);

        f1 = Float.parseFloat(s1);
        f2 = Float.parseFloat(s1);

        System.out.println("字符串拼接结果为:"+joint(s1, s2));
        System.out.println("字符串转换为整数相加结果为:"+joint(i1, i2));
        System.out.println("字符串转换为浮点数相加结果为:"+joint(f1, f2));
    }

    public static String joint(String s1, String s2) {
        return s1+s2;
    }

    public static String joint(int i1, int i2) {
        return String.valueOf(i1+i2);
    }
    public static String joint(float f1, float f2) {
        return String.valueOf(f1+f2);
    }
}

Java每日一题:5

    应用 FilelnputStream 类,编写应用程序,从磁盘上读取一个 Java 程序,并将源程序代码显示在屏幕上。( 被读取的文件路径可自行定义)

public class Exer6 {
    public static void main(String[] args) {
        //创建文件输入流
        FileInputStream fileInputStream = null;
        try {
            //注意此处位于main方法中,相对路径的起始位置为根目录(整个工程)
            fileInputStream = new FileInputStream("src/com/java/meiriyiti/exer6/Hello.java");
            int read = fileInputStream.read();
            while (read != -1) {
                System.out.print((char)read);
                read = fileInputStream.read();
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                assert fileInputStream != null;
                //关闭资源
                fileInputStream.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}

Java每日一题:6
        编写一个 Java 程序将 100,101,102,103,104,105
 以数组的形式写入到 Dest.txt 文件中,并以相反的顺序读出
*显示在屏幕上。

public class Exer7 {
    public static void main(String[] args) {
        int[] arr = new int[]{100, 101, 102, 103, 104, 105};
        DataOutputStream dos = null;
        DataInputStream dis = null;
        try {
            dos = new DataOutputStream(new FileOutputStream("src/com/java/meiriyiti/exer7/dest.txt"));
            for (int j : arr) {
                dos.write(j);
            }
            dis=new DataInputStream(new FileInputStream("src/com/java/meiriyiti/exer7/dest.txt"));
            for (int i = arr.length-1; i >=0 ; i--) {
                arr[i] = dis.read();
            }
            for (int i : arr) {
                System.out.println(" " + i);
            }
            System.out.println();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                assert dos != null;
                dos.close();
                assert dis != null;
                dis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Java每日一题:7

        编写一个 Java程序实现多线程,在线程中输出线程的名
*字,隔300毫秒输出一次,共输出20次。

public class Exer8 {
    public static void main(String[] args) {
        ThreadDemo threadDemo1 = new ThreadDemo("T1");
        ThreadDemo threadDemo2 = new ThreadDemo("T2");
        ThreadDemo threadDemo3 = new ThreadDemo("T3");
        threadDemo1.start();
        threadDemo2.start();
        threadDemo3.start();

    }
}

class ThreadDemo extends Thread {
    public ThreadDemo(String name) {
        super(name);
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println(" " + getName());
            try {
                sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println(getName()+"/end");
    }
}

  • 4
    点赞
  • 90
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

影流小白

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

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

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

打赏作者

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

抵扣说明:

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

余额充值