偶是一个JAVA的初学者,最近看到这样的一个编程题目:
输出某年某月的日历页,通过main方法的参数将年份和月份时间传递到程序中.
请问各位应该怎样编写呀!望各们大虾们多多指教了!小北先谢过了!
想了很久,终于写出来了,各位大虾点评一下!
import java.util.*;
import java.io.*;
class CalendarTest {
static int strToInt;
static String s="";
public static void getInfo()
{
try
{
BufferedReader cin=new BufferedReader (new InputStreamReader(System.in));
s=cin.readLine();
strToInt=Integer.parseInt(s);
}catch(Exception e){};
}
public static void main(String[] args)
{
int year;
int month;
Calendar calendarObj=Calendar.getInstance();
System.out.print("请输入你想查询日历的年份:");
getInfo();
year=strToInt;
System.out.println();
System.out.print("请输入你想查询日历的月份:");
getInfo();
month=strToInt;
calendarObj.set(year,month-1,1);
int weeks=calendarObj.get(Calendar.DAY_OF_WEEK)-1;
System.out.println();
System.out.println(year+"年"+month+"月的日历如下:");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println('日'+" "+'一'+" "+'二'+" "+'三'+" "+'四'+" "+'五'+" "+'六');
String[] count=new String[weeks+31];
for(int i=0;i<weeks;i++)
{
count[i]="**";
}
for(int i=weeks,n=1;i<weeks+31;i++)
{
if(n<=9)
count[i]=" "+String.valueOf(n);
else
count[i]=String.valueOf(n);
n++;
}
for(int i=0;i<weeks+31;i++)
{
if(i%7==0)
{
System.out.println();
System.out.print(count[i]+" ");
}
else
System.out.print(count[i]+" ");
}
System.out.println();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
}
}