dポイントプレゼントキャンペーン実施中!

西暦年号・月を入力して、その月のカレンダーを出力するというものです。
<処理例>
指定日のカレンダーを出力します。
西暦年を入力:2007
月を入力:6
日 月 火 水 木 金 土
______________________1____2
_3___4___5___6___7___8___9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

というものを出力したいです。下線は何もないという意味です。(見にくかったらすみません)

一応途中のプログラム載せます。 ~~~の部分を教えてください。
お願いします。

import java.io.*;

public class Ex01a{
public static void main ( String[] args ) throws IOException{
int year, month, day, i, youbiNum=0;
String ss;
BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in));
System.out.println("指定月のカレンダーを出力します");

System.out.print("西暦年を入力:");
ss = kbd.readLine();
year = getInt(ss);

System.out.print("月を入力:");
ss = kbd.readLine();
month = getInt(ss);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
day = 1;
youbiNum=getyoubi(year,month,day);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
public static void printCal(int maxDay, int youbiNum){
int cal[] = new int[43];

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


public static int getyoubi(int year, int month, int day){
int youbiNum, y1, nissuu=0;

y1 = year-1;
nissuu += y1 + y1/4 - y1/100 + y1/400+getJulian(year, month, day);

youbiNum = nissuu % 7;

return youbiNum;
}

public static int getJulian(int year, int month, int day){
int julian;
julian = day;
while(--month > 0){
julian+=getDay(year,month);
}
return julian;
}

public static int getDay(int year, int month){
int mmdd[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int mmdd_day;

mmdd[2] += uruu(year);
mmdd_day = mmdd[month];

return mmdd_day;
}

public static int getInt(String ss){
try{
return Integer.parseInt(ss);
}
catch(Exception e){
return 0;
}
}

public static int uruu(int yy){
if(yy%4==0 && yy%100!=0 || yy%400==0) return 1;
else return 0;
}
}

A 回答 (2件)

#1です



課題か何かですか??
ちょっと求めている答えと違うかもしれませんが、以下にソースを載せます
何かしら読み取ってもらえれば幸いです
---------------サンプル---------------
import java.io.*;

public class Ex01a {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("指定月のカレンダーを出力します");

System.out.print("西暦年を入力:");
String line = reader.readLine();
int year = toInt(line);

System.out.print("月を入力:");
line = reader.readLine();
int month = toInt(line);

printCal(year, month);
}

public static void printCal(int year, int month) {
int firstDayOfWeek = getDayOfWeek(year, month, 1);
int lastDay = getLastDay(year, month);

System.out.println("日\t月\t火\t水\t木\t金\t土");
int dayOfWeek = 0;

for(int i = 0; i < firstDayOfWeek; i++){
System.out.print(" \t");
dayOfWeek++;
}

for(int i = 1; i <= lastDay; i++){
System.out.print(i + "\t");
dayOfWeek++;
if(dayOfWeek == 7){
System.out.println();
dayOfWeek = 0;
}
}
}

/*
* 指定した年月日の曜日を求める。戻り値と曜日の関係は
* 日曜日:0~土曜日:6
*/
public static int getDayOfWeek(int year, int month, int day) {
if(month <= 2){
month += 12;
year--;
}
int h = year/100, y = year%100;

// ツェラーの公式
int dayOfWeek = y + y/4 + h/4 - 2*h + 13*(month+1)/5 + day;
return (dayOfWeek+6)%7;
}

// 指定した年・月の最終日を返す
public static int getLastDay(int year, int month) {
int[] maxDay = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

if(isLeapYear(year)) maxDay[2]++;
return maxDay[month];
}

// 引数の文字列をint型に変換する。数字で無かった場合は0を返す。
public static int toInt(String ss) {
try {
return Integer.parseInt(ss);
} catch (Exception e) {
return 0;
}
}

// 引数の年がうるう年かどうか判定する
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
}
}

-----------------終了-----------------
    • good
    • 1
この回答へのお礼

ありがとうございます。
課題なんですよ。

これでなんとかやってみます。

お礼日時:2008/07/05 16:03

こんにちは



Javaにはjava.util.Calendarクラスという便利なクラスがあります。
もちろん質問者さんのように曜日計算などのメソッドを書いても良いですが、Calendarクラスを使ったほうが全体もすっきりとして良いと思います。


参考までに

参考URL:http://java.sun.com/j2se/1.5.0/ja/docs/ja/api/ja …
    • good
    • 0
この回答へのお礼

java.util.Calendarクラスというのがあるんですね。

できればそれを使わずに作成したいんです。
お願いします。

お礼日時:2008/07/03 19:22

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています