Java具有每日记账功能的简易日历程序
时间: 2024-02-25 21:53:30 浏览: 112
好的,我可以为你提供一个简单的Java日历程序,它可以记录每日收支并生成日历图表。以下是程序代码:
```java
import java.util.Scanner;
public class CalendarApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份:");
int year = scanner.nextInt();
System.out.print("请输入月份:");
int month = scanner.nextInt();
// 计算该月份的天数
int daysInMonth = getDaysInMonth(year, month);
// 打印日历头部
System.out.println(" 日 一 二 三 四 五 六");
// 记录每日收支
int[] incomes = new int[daysInMonth];
int[] expenses = new int[daysInMonth];
for (int day = 1; day <= daysInMonth; day++) {
// 打印空格
for (int i = 1; i < day; i++) {
System.out.print(" ");
}
// 打印日期
System.out.printf("%2d ", day);
// 输入当日收支
System.out.print("收支:");
int income = scanner.nextInt();
incomes[day - 1] = income;
System.out.print("支出:");
int expense = scanner.nextInt();
expenses[day - 1] = expense;
}
// 打印每日收支表格
System.out.println("\n每日收支:");
System.out.println("日期\t收入\t支出\t余额");
int balance = 0;
for (int i = 0; i < daysInMonth; i++) {
balance += incomes[i];
balance -= expenses[i];
System.out.printf("%d\t%d\t%d\t%d\n", i + 1, incomes[i], expenses[i], balance);
}
}
// 获取指定月份的天数
public static int getDaysInMonth(int year, int month) {
int daysInMonth;
if (month == 2) {
if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
daysInMonth = 30;
} else {
daysInMonth = 31;
}
return daysInMonth;
}
}
```
你可以根据需要进行修改和扩展。
阅读全文