「Java学习打卡」48、程序练习

创建一个程序,从控制台读取文本,并将其写入到文件中。用户输入一个空行,表示结束输入。

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        File file;
        FileWriter fw = null;
        file = new File("/usr/Test.txt");
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            fw = new FileWriter(file);
            Scanner in = new Scanner(System.in);
            while (true) {
                String s = in.nextLine();
                if (s.equals(""))
                    break;
                fw.write(s);
                fw.flush();
            }
            System.out.println("写入数据成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

一个这样的程序:让用户输入其全名,年龄和电话号码,以特定的格式显示这些消息,并显示用户的姓名的首字母。

import java.util.Scanner;

class File0 {
    private String name;
    private int age;
    private String tel;
    private char sig;

    File0(String name, int age, String tel) {
        name(name);
        age(age);
        tel(tel);
        getFirstChar(name);
    }

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

    void age(int age) {
        this.age = age;
    }

    void tel(String tel) {
        this.tel = tel;
    }

    void print() {
        System.out.print(this.sig + "\t姓名" + this.name);
        System.out.print("\t年龄" + this.age);
        System.out.print("\t电话" + this.tel);
    }

    private void getFirstChar(String str) {//方法一
        this.sig = str.charAt(0);// 索引下标为0的字符
    }

}


public class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("输入姓名: ");
        String n = scan.nextLine();
        System.out.print("输入年龄: ");
        int a = scan.nextInt();
        Scanner scan0 = new Scanner(System.in);
        System.out.print("输入电话: ");
        String t = scan0.nextLine();
        File0 f = new File0(n, a, t);
        f.print();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现每天打卡人数的统计,可以考虑以下步骤: 1. 定义一个打卡记录类,包含打卡人的姓名和打卡时间等信息。 2. 在程序创建一个日期变量,用于记录当前日期。 3. 创建一个打卡记录列表,用于存储所有打卡记录。 4. 在程序运行时,读取打卡记录文件,将其打卡记录加入打卡记录列表。 5. 遍历打卡记录列表,对每个记录进行处理:如果记录的日期等于当前日期,则将该记录的姓名计入当天打卡人数。 6. 将当天打卡人数输出或保存到文件。 下面是一个示例程序,可以作为参考: ```java import java.io.*; import java.text.*; import java.util.*; public class Attendance { // 打卡记录类 static class Record { String name; // 打卡人姓名 Date time; // 打卡时间 public Record(String name, Date time) { this.name = name; this.time = time; } } public static void main(String[] args) { // 创建日期变量 Date today = new Date(); // 创建打卡记录列表 List<Record> records = new ArrayList<>(); // 读取打卡记录文件 try (BufferedReader reader = new BufferedReader(new FileReader("attendance.txt"))) { String line; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); while ((line = reader.readLine()) != null) { String[] fields = line.split(","); String name = fields[0]; Date time = dateFormat.parse(fields[1]); records.add(new Record(name, time)); } } catch (IOException | ParseException e) { e.printStackTrace(); } // 统计当天打卡人数 int count = 0; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); for (Record record : records) { if (dateFormat.format(record.time).equals(dateFormat.format(today))) { count++; } } // 输出当天打卡人数 System.out.println("今天打卡人数:" + count); } } ``` 在示例程序,我们先定义了一个打卡记录类 Record,包含打卡人姓名和打卡时间两个属性。然后在程序创建了一个日期变量 today,用于记录当前日期;创建了一个打卡记录列表 records,用于存储所有打卡记录。 在读取打卡记录文件时,我们使用了 BufferedReader 和 FileReader 两个类,逐行读取记录文件并解析每行数据,将解析出的打卡记录加入到打卡记录列表。 接下来,我们使用 SimpleDateFormat 类将日期格式化为 yyyy-MM-dd 的形式,遍历打卡记录列表,对每个记录进行处理,如果记录的日期等于当前日期,则将该记录的姓名计入当天打卡人数。 最后,输出当天打卡人数。注意,这里我们使用了 System.out.println() 方法将果输出到控制台,你可以将其改为将果保存到文件,具体实现方式可以参考 Java 的文件操作相关内容
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值