编写一个程序,输出一个月的日历。
2013-07-30 17:41
573 查看
方法一:
import java.util.Calendar;
import java.util.Scanner;
public class YueLi {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();//a表示你将要输入几个年月
sc.nextLine();
Calendar calendar = Calendar.getInstance();
for(int i = 0; i < a; i++){
String s = sc.nextLine();
String[] strs = s.split("-");
int year = Integer.parseInt(strs[0]);
int month = Integer.parseInt(strs[1]);
calendar.set(Calendar.YEAR, year); //设置年
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.DATE, 1);
//System.out.println(calendar.get(7));
for(int j =1; j < calendar.get(Calendar.DAY_OF_WEEK); j++) {
System.out.print(" ");//输出开始前面的空格
}
for(int k = 1; k <= whichMonth(year, month); k++) {
calendar.set(Calendar.DATE, k);
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
System.out.println();
if(k <= 10) { //为了输出对齐
System.out.print(k+" ");
} else {
System.out.print(k+ " ");
}
}
}
}
public static int whichMonth(int year, int month) {
int days = 0;
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {//润年
days = 29;
} else {
days = 28;
}
break;
}
return days;
}
}
方法二:
import java.util.Scanner;
public class DisplayYueLi{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String input1=sc.nextLine();
String[] input2=input1.split("-");
int year=Integer.parseInt(input2[0]);
int month=Integer.parseInt(input2[1]);
int fristDay=GetfristDay(year,month);//
int mDay=GetmDay(year,month);
DisplayYueLi(fristDay,mDay);
}
static int GetfristDay(int year,int month){
int zDays=0;
for(int i=1;i<year;i++){
if(i%4==0&&i%100!=0||i%400==0){
zDays=zDays+366;
}
else{
zDays=zDays+365;
}
}
for(int j=1;j<month;j++){
switch(j){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
zDays=zDays+31;
break;
case 4:
case 6:
case 9:
case 11:
zDays=zDays+30;
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0){
zDays=zDays+29;
}
else{
zDays=zDays+28;
}
}
}
int fristDay=(int)(zDays+1)%7;
// System.out.println("fristDay=" +fristDay);
return(fristDay);
}
static int GetmDay(int year,int month){
int mDay=0;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
mDay=31;
break;
case 4:
case 6:
case 9:
case 11:
mDay=30;
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0){
mDay=29;
}
else{
mDay=28;
}break;
}
return(mDay);
}
static void DisplayYueLi(int fristDay,int mDay){
String space=" ";
System.out.println(space+"日"+space+"一"+space+"二"+space+"三"+space+"四"+space+"五"+space+"六");
for(int m=0;m<fristDay;m++){
System.out.print(space+" ");
}
for(int n=1;n<=mDay;n++){
if((n+fristDay)%7==0&&n!=1){
if (n<10)
System.out.println(space+" "+n);
else
System.out.println(space+n);
}
else{
if (n<10)
System.out.print(space+" "+n);
else
System.out.print(space+n);
}
}
}
}
import java.util.Calendar;
import java.util.Scanner;
public class YueLi {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();//a表示你将要输入几个年月
sc.nextLine();
Calendar calendar = Calendar.getInstance();
for(int i = 0; i < a; i++){
String s = sc.nextLine();
String[] strs = s.split("-");
int year = Integer.parseInt(strs[0]);
int month = Integer.parseInt(strs[1]);
calendar.set(Calendar.YEAR, year); //设置年
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.DATE, 1);
//System.out.println(calendar.get(7));
for(int j =1; j < calendar.get(Calendar.DAY_OF_WEEK); j++) {
System.out.print(" ");//输出开始前面的空格
}
for(int k = 1; k <= whichMonth(year, month); k++) {
calendar.set(Calendar.DATE, k);
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
System.out.println();
if(k <= 10) { //为了输出对齐
System.out.print(k+" ");
} else {
System.out.print(k+ " ");
}
}
}
}
public static int whichMonth(int year, int month) {
int days = 0;
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {//润年
days = 29;
} else {
days = 28;
}
break;
}
return days;
}
}
方法二:
import java.util.Scanner;
public class DisplayYueLi{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String input1=sc.nextLine();
String[] input2=input1.split("-");
int year=Integer.parseInt(input2[0]);
int month=Integer.parseInt(input2[1]);
int fristDay=GetfristDay(year,month);//
int mDay=GetmDay(year,month);
DisplayYueLi(fristDay,mDay);
}
static int GetfristDay(int year,int month){
int zDays=0;
for(int i=1;i<year;i++){
if(i%4==0&&i%100!=0||i%400==0){
zDays=zDays+366;
}
else{
zDays=zDays+365;
}
}
for(int j=1;j<month;j++){
switch(j){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
zDays=zDays+31;
break;
case 4:
case 6:
case 9:
case 11:
zDays=zDays+30;
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0){
zDays=zDays+29;
}
else{
zDays=zDays+28;
}
}
}
int fristDay=(int)(zDays+1)%7;
// System.out.println("fristDay=" +fristDay);
return(fristDay);
}
static int GetmDay(int year,int month){
int mDay=0;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
mDay=31;
break;
case 4:
case 6:
case 9:
case 11:
mDay=30;
break;
case 2:
if(year%4==0&&year%100!=0||year%400==0){
mDay=29;
}
else{
mDay=28;
}break;
}
return(mDay);
}
static void DisplayYueLi(int fristDay,int mDay){
String space=" ";
System.out.println(space+"日"+space+"一"+space+"二"+space+"三"+space+"四"+space+"五"+space+"六");
for(int m=0;m<fristDay;m++){
System.out.print(space+" ");
}
for(int n=1;n<=mDay;n++){
if((n+fristDay)%7==0&&n!=1){
if (n<10)
System.out.println(space+" "+n);
else
System.out.println(space+n);
}
else{
if (n<10)
System.out.print(space+" "+n);
else
System.out.print(space+n);
}
}
}
}
相关文章推荐
- 编写一个程序,输出一个字符串中的大写英文字母数,小写英文字母数以及非英文字母数
- 编写函数把华氏温度转换为摄氏温度,公式为:C = (F - 32) * 5/9 在主程序中提示用户输入一个华氏温度,转化后输出相应的摄氏温度。
- 打印不同的数(编写一个程序,使用指针访问数组方式,读入十个数,输出其中不同的数,即一个数如果出现多次,只打印一次)
- Java 实训4 编写一个窗体程序显示日历
- 编写一个程序,可以一直接收键盘字符,如果是小写字符就输出对应的大写字符,如果接收的是大写字符,就输出对应的小写字符,如果是数字不输出。
- 小程序:编写一个程序,将数组a中的相同的数据删除的只剩一个,然后输出;
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。
- 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
- 利用c语言编写程序输出一个数的每一位(多种方法)
- 给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。输入值小于1000。
- c语言:编写一个程序,输入a,b,c三个值,输出其中最大者
- 通过键盘输入100 以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 3. 编写一个C程序,实现对10个整数进行升序排序输出。
- 编写程序,自定义一函数,用来判断一个整数是否为素数,主函数输入一个数,输出是否为素数
- 【C语言】编写一个程序从标准输入读取字符,并把他们写到标准输出。除了大写字母转换成小写字母之外,其他的原样输出。
- 编写一个程序,使用fork函数来创建一个子进程,并分别输出父子进程的进程ID
- 编写一个程序,输出当年的月历。
- 文章标题编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。
- java程序,编写一个方法,输出在一个字符串中,指定字符串输出的次数
- 用80x86汇编语言,编写一个输出16进制数字的程序