用java输入年份月份输出,该月第一天是星期几。

用java编写:输入任意年份和月份,输出对应月份的天数。~

用 java编写:输入任意年份和月份,输出对应月份的天数,首先判断输入年份是否是闰年,然后使用switch 方法判断月份,判断代码如下:
public class GetDays {
public static int getDays(int year, int month) {int days = 0;boolean isLeapYear = false;if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {System.out.println("这一年是闰年");isLeapYear = true;} else {System.out.println("这一年不是闰年");isLeapYear = false;}switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:days = 31;break;case 2:if (isLeapYear) {days = 29;} else {days = 28;}break;case 4:case 6:case 9:case 11:days = 30;break;default:System.out.println("error!!!");break;}return days;}}

扩展资料在java 语言中switch 语法的使用规则为:
1、switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。
2、switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。
3、case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
3、当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。
参考资料:百度百科—switch

给你一个代码吧:
import java.util.Scanner;

public class wnl {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
int totalDay=0; //计算总天数
int dayOfWeek;//保存当月第一天是星期几
int day=0 ; //当月的天数
int dayOfYear=0; //保存用户输入的年月之前的天数
Scanner cs=new Scanner(System.in);
System.out.print("请输入年:");
int year=cs.nextInt();
System.out.print("请输入月:");
int month=cs.nextInt();
boolean bool=false;
// 判断输入的年份是否是闰年备用
if(year%4==0&&year%100!=0||year%400==0){
bool=true;
}
// 计算出到用户输入的年份1月1日之前的总天数
for(int i=1900;i<year;i++){
if(i%4==0&&i%100!=0||i%400==0){
totalDay+=366;
}else{
totalDay+=365;
}
}
for(int i=1;i<=month;i++){
switch(i){ //根据月不同给day赋值进行计算
case 1: //同时day还会用于保存当月天数后面的输出
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:day=31;break;
case 4:
case 6:
case 9:
case 11:day=30;break;
// 根据是否闰年来决定2月多少天
case 2:if(bool){
day=29;
break;
}else{
day=28;
break;
}
//在获得输入月份之前所有天数的同时
//还获得了输入月份的天数备用
}
if(i<month){
dayOfYear+=day;//把用户输入的月份之前的所有天
} //数进行加合
}
//将本年的天数加在之前计算的总天数上
totalDay+=dayOfYear;
// 把总天数对7取余计算出当月第一天是星期几
dayOfWeek=(1+totalDay)%7;

System.out.println("星期天星期一星期二星期三星期四星期五星期六");
// 输出空格
for(int i=0;i<dayOfWeek;i++){
System.out.print("");
}
for(int i=1;i<=day;i++){
//控制每到星期6就换行
//原理为总天数加上当月天数对7取余
if((totalDay+i)%7==6){
System.out.print(i+"
");
}else{
System.out.print(i+"");
}
}
}
}

import java.util.Scanner;

public class wnl {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
int totalDay=0; //计算总天数
int dayOfWeek; //保存当月第一天是星期几
int day=0 ; //当月的天数
int dayOfYear=0; //保存用户输入的年月之前的天数
Scanner cs=new Scanner(System.in);
System.out.print("请输入年:");
int year=cs.nextInt();
System.out.print("请输入月:");
int month=cs.nextInt();
boolean bool=false;
// 判断输入的年份是否是闰年备用
if(year%4==0&&year%100!=0||year%400==0){
bool=true;
}
// 计算出到用户输入的年份1月1日之前的总天数

用Java编写 输入年与月,来判定该月一共有多少天 比如2008年2月 2008年...
答:System.out.println(year + "年" + month + "月共有" + maxDay + "天");} } 在上述代码中,我们先通过Scanner类获取用户输入的年份和月份,然后调用JudgeDay类的dayOfMonth()方法获取该年月的天数,并输出该月的天数。需要注意的是,在调用dayOfMonth()方法时,需要使用类名.方法名的方式进...

Java编写程序,输入年份,输出本年度各月份日历
答:import java.util.Calendar;import java.util.Scanner;public class Test { static public void main(String 参数[]){ Calendar c = Calendar.getInstance();Scanner sc = new Scanner(System.in);System.out.println("请输入年份:");int year= sc.nextInt();c.set(Calendar.YEAR, year);c.set...

编写程序,提示用户输入月份和年份,然后显示这个月份的天数.java语言编 ...
答:import java.util.Scanner;public class $ { private static int[] DAYS = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("输入年份:"); int year =...

用java输入年份月份输出,该月第一天是星期几。
答:int dayOfYear=0; //保存用户输入的年月之前的天数 Scanner cs=new Scanner(System.in);System.out.print("请输入年:");int year=cs.nextInt();System.out.print("请输入月:");int month=cs.nextInt();boolean bool=false;// 判断输入的年份是否是闰年备用 if(year%4==0&&year%100!=0||...

Java题,输入年份与月份,输出日历,求大神,我用的是myEcliprise. ,有...
答:import java.util.Calendar;import java.util.Date;import java.util.Scanner;public class MyCalendar{public static void main(String[] args){Scanner scanner = new Scanner(System.in);String reg = "^(\\d+)[^\\d]+((0?[1-9])|(1[012]))$";while(true){System.out.println("输入...

用java编写:输入任意年份和月份,输出对应月份的天数。
答:用 java编写:输入任意年份和月份,输出对应月份的天数,首先判断输入年份是否是闰年,然后使用switch 方法判断月份,判断代码如下:public class GetDays { public static int getDays(int year, int month) {int days = 0;boolean isLeapYear = false;if (((year % 4 == 0) && (year % 100 !...

输入年份月份,打印输出当月日历 用java
答:public class java { public static void main(String[] args) throws IOException { BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));System.out.println("请输入年份:");String s1=buf.readLine();System.out.println("请输入月份: ");String s2=buf.readLine();int ...

java Date类型 按年,月,日 取出并输出 求高手.
答:Date date = new java.sql.Date(1319534374312l);;cld.setTime(date);/ 注:在jdk1.6以后下列方法都已过时 date.getYear();date.getMonth();date.getDay();/ System.out.println("日期为:"+date.toString());//方法一 System.out.println("年份:"+cld.get(Calendar.YEAR));System.out....

在java里怎么做 从键盘接收4位数字的年份和月份,并判断输出该年该月...
答:import java.util.Scanner;public class countday { / param args / public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in);System.out.println("请输入年份");int year = input.nextInt();System.out.println("请输入月份");...

用java编程实现:产生一个1-12的数,并根据随机数的值输出对应月份的名称...
答:switch (month) { case 1:return "一月";case 2:return "二月";case 3:return "三月";case 4:return "四月";case 5:return "五月";case 6:return "六月";case 7:return "七月";case 8:return "八月";case 9:return "九月";case 10:return "十月";case 11:return "十一月";default...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网