輸入本年的某個(gè)月后,屏幕輸出這個(gè)月的月歷,每星期一行,從星期日開始,到星期六結(jié)束。
成都創(chuàng)新互聯(lián)公司是專業(yè)的賓川網(wǎng)站建設(shè)公司,賓川接單;提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行賓川網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
import java.util.*;
public class Year{
/**
*判斷是否為閏年
*/
public boolean getIsRun(int year){
if((year%4==0 year %100 !=0)||(year%400==0)){
return true;
}
return false;
}
/**
*返回某年某月有多少天。
*/
public int getDay(int year ,int month){
int[] day={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//閏年月份
int[] day1={31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//普通月份
if(this.getIsRun(year)){
return day[month+1];
}else{
return day1[month+1];
}
}
/**
*返回這個(gè)月的1號(hào)是星期幾
*/
public int getWeek(int year,int month){
Date date=new Date();
date.setYear(year);
date.setMonth(month);
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
return calendar.get(calendar.DAY_OF_WEEK);
}
/**
*打印出來,嘎嘎
*/
public void showPrint(int year,int month){
int day=this.getDay(year,month);
int week=this.getWeek(year,month);
System.out.println("日\(chéng)t一\t二\t三\t四\t五\t六\n");
for(int i=0;i=week;i++){//
System.out.print (" \t");
}
for(int j=1;j=day;j++){
if((j+week)%7==0){
System.out.println ();
}
System.out.print (j+"\t");
}
// System.out.println(week);
}
public static void main(String[] args){
Year y=new Year();
y.showPrint(2008,3);
}
}
package mycalendar;
import java.util.*;
class ViewMonth {
int month;
int year;
ViewMonth(final int displayMonth, final int displayYear) {
month = displayMonth;
year = displayYear;
}
private String checkMonth() {
String[] months = {
"1 月" , "2 月" , "3 月",
"4 月" , "5 月" , "6 月",
"7 月" , "8 月" , "9 月",
"10 月" , "11 月" , "12 月"
};
return months[month];
}
private int checkDays() {
int[] numofDays = {
31, 28, 31,
30, 31, 30,
31, 31, 30,
31, 30, 31
};
return numofDays[month];
}
/**
* 使用此方法打印該月的日歷.
*/
void printMonth() {
/* 將該月份起始處的天數(shù)留空. */
int initialSpaces = 0;
try {
/* 獲取月份名稱. */
String monthName = checkMonth();
System.out.println();
System.out.println("\t\t\t " + year + " 年 " + monthName );
System.out.println();
} catch (ArrayIndexOutOfBoundsException ae) {
System.out.println("超出范圍 ..........");
System.exit(0);
}
GregorianCalendar cal = new GregorianCalendar(year, month, 1);
System.out.println("\t日\(chéng)t一\t二\t三\t四\t五\t六");
initialSpaces = cal.get(Calendar.DAY_OF_WEEK) - 1;
/* 獲取天數(shù). */
int daysInMonth = checkDays();
/* 檢查是否為閏年并為二月增加一天. */
if (cal.isLeapYear(cal.get(Calendar.YEAR)) month == 1) {
++daysInMonth;
}
for (int ctr = 0; ctr initialSpaces; ctr++) {
System.out.print("\t");
}
for (int ctr = 1; ctr = daysInMonth; ctr++) {
/* 為單個(gè)日期添加空格. */
if (ctr = 9) {
System.out.print(" ");
}
System.out.print("\t" + ctr);
/* 檢查行的末尾. */
if ((initialSpaces + ctr) % 7 == 0) {
System.out.println();
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
class J7上機(jī)2 {
protected J7上機(jī)2() {
}
public static void main(String[] args) {
int month, year;
if (args.length == 2) {
System.out.println("顯示日歷");
System.out.println();
int mon = Integer.parseInt(args[0]);
month = mon - 1;
year = Integer.parseInt(args[1]);
} else {
Calendar today = Calendar.getInstance();
month = today.get(Calendar.MONTH);
year = today.get(Calendar.YEAR);
}
ViewMonth mv = new ViewMonth(month, year);
mv.printMonth();
}
}
給你
改了下,終于改好了。。。
樓上改的還是有點(diǎn)問題,因?yàn)樗绦虿⒉荒苷_顯示日期。。。
1.數(shù)組ARR沒有初始化。
2.F1和F2沒有初始化。
3.打印日期的邏輯有問題。
4.計(jì)算var的邏輯有問題。
package test;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
public class Cal extends Applet implements ActionListener {
Label L1 = new Label("輸入年份");
Label L2 = new Label("輸入月份");
TextField F1 = new TextField(5);
TextField F2 = new TextField(5);
Button B1 = new Button("查詢");
int arr[] = new int[12];
String ARR[];
int var;
public void init() {
add(L1);
add(F1);
add(L2);
add(F2);
add(B1);
Date date = new Date();
F1.setText(String.valueOf(date.getYear() + 1900));
F2.setText(String.valueOf(date.getMonth() + 1));
B1.addActionListener(this);
arr[0] = arr[2] = arr[4] = arr[6] = arr[7] = arr[9] = arr[11] = 31;
arr[3] = arr[5] = arr[8] = arr[10] = 30;
date.setDate(1);
var = date.getDay();
}
public void actionPerformed(ActionEvent e) {
int x = Integer.parseInt(F1.getText()), i = Integer.parseInt(F2
.getText()) - 1, s = 0;
Date date = new Date();
date.setYear(x - 1900);
date.setMonth(i);
date.setDate(1);
var = date.getDay();
repaint();
}
public void paint(Graphics g) {
int xpos, ypos;
arr[0] = arr[2] = arr[4] = arr[6] = arr[7] = arr[9] = arr[11] = 31;
arr[3] = arr[5] = arr[8] = arr[10] = 30;
int x = Integer.parseInt(F1.getText()), i = Integer.parseInt(F2
.getText()) - 1;
if ((x % 4 == 0 x % 100 != 0) || (x % 400 == 0))
arr[1] = 29;
else
arr[1] = 28;
g.drawString("星期一", 50, 50);
g.drawString("星期二", 100, 50);
g.drawString("星期三", 150, 50);
g.drawString("星期四", 200, 50);
g.drawString("星期五", 250, 50);
g.drawString("星期六", 300, 50);
g.drawString("星期日", 350, 50);
ARR = new String[arr[i]];
int j = 0, k = 1;
while (k = arr[i]) {
ARR[j] = String.valueOf(k);
k ++;
j ++;
}
int line = 0;
for (int m = 0 ; m ARR.length ; m ++) {
ypos = 50 + 50 * (line + 1);
xpos = (var * 50 + 50 * (m % 7)) % 350;
if (xpos == 0) {
xpos = 350;
line ++;
}
g.drawString(ARR[m], xpos, ypos);
}
}
}