「Java学习打卡」46、4.10练习

定义一个行数和列数相等的二维数组,并执行初始化,然后计算该数级两条对角线上的元素值之和。

public class Test {

    public static void main(String[] args) {
        int i, s = 0, w = 0;

        int[][] a ={{2, 6, 2},{4, 9, 2},{2, 6, 1}} ;

        int n = 3;
        for (i = 0; i < n; i++) {
            s = s + a[i][i];
            w = w + a[i][n - 1 - i];
        }
        System.out.println(s);
        System.out.print(w);

    }
}

编写程序新建一文本文件,并对该文件进行复制操作

import java.util.*;
import java.io.*;

class FileControl{
    public void create(String Save){
        FileOutputStream output;
        File out = new File(Save);
        System.out.println("输入文件内容:");
        Scanner in = new Scanner(System.in);
        try {
            output = new FileOutputStream(out);
            byte input[] = in.nextLine().getBytes();
            output.write(input);
            output.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void copy(String file0,String file1) throws IOException{

        FileInputStream read0 = new FileInputStream(file0);
        byte[] ch = new byte[1024];
        FileOutputStream write0 = new FileOutputStream(file1);
        int m = 0;
        while ((m = read0.read(ch)) != -1) {
            write0.write(ch, 0, m);
        }
        read0.close();
        write0.close();
    }

}
public class Test {
    public static void main(String[] args) throws IOException {
    FileControl f=new FileControl();
    f.create("src/step4/input/input.txt");
    f.copy("src/step4/input/input.txt","src/step4/output/output.txt");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java员工打签到代码的实现方式有很多种,以下是其中一种可能的实现方式: ```java import java.util.Date; public class Employee { private String name; private Date lastSignIn; public Employee(String name) { this.name = name; } public void signIn() { Date now = new Date(); System.out.println(name + "签到成功,时间:" + now); lastSignIn = now; } public void signOut() { Date now = new Date(); System.out.println(name + "签退成功,时间:" + now); } public void checkInStatus() { if (lastSignIn == null) { System.out.println(name + "尚未签到"); } else { System.out.println(name + "上次签到时间:" + lastSignIn); } } } ``` 上面的代码定义了一个`Employee`类,其中包含了员工的姓名和上次签到时间。类中有三个方法:`signIn()`、`signOut()`和`checkInStatus()`。`signIn()`方法表示员工签到,会打印出员工姓名和当前时间,并将当前时间记录为上次签到时间;`signOut()`方法表示员工签退,会打印出员工姓名和当前时间;`checkInStatus()`方法表示查询员工的签到状态,会打印出员工姓名和上次签到时间(如果已经签到过),否则会提示尚未签到。 如果要使用这段代码,可以在其他类中创建`Employee`对象,并调用其中的方法来完成打签到功能。例如: ```java public class Main { public static void main(String[] args) { Employee emp1 = new Employee("张三"); emp1.signIn(); emp1.checkInStatus(); emp1.signOut(); } } ``` 这段代码创建了一个名为`emp1`的`Employee`对象,姓名为“张三”。接着调用了`signIn()`方法进行签到,`checkInStatus()`方法查询签到状态,最后调用了`signOut()`方法进行签退。运行这段代码后,会打印出以下结果: ``` 张三签到成功,时间:Thu Jul 22 14:47:23 CST 2021 张三上次签到时间:Thu Jul 22 14:47:23 CST 2021 张三签退成功,时间:Thu Jul 22 14:47:28 CST 2021 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值