java编写程序根据输入的年和月计算指定月的总天数,要代码
发布网友
发布时间:2023-09-28 03:02
我来回答
共4个回答
热心网友
时间:2024-03-13 19:28
好了,你试一下
import java.util.*;
class Test
{
public static void main(String[] args)
{
try
{
Scanner in=new Scanner(System.in);
System.out.print("请输入年份:");
int year=in.nextInt();
System.out.print("请输入月份:");
int month=in.nextInt();
if(year<0 || month<0)
System.out.println("你输入的年份或月份为负,请输入正整数");
int day=28;
GregorianCalendar gc=new GregorianCalendar(year,month-1,day);
System.out.println(gc.get(Calendar.MONTH)+" "+gc.get(Calendar.DAY_OF_MONTH));
while((month-1)==gc.get(Calendar.MONTH))
{
gc.add(Calendar.DAY_OF_MONTH,1);
day++;
}
System.out.println("该月的总天数为:"+(--day));
}
catch(Exception ex)
{
System.out.println("您输入了非法字符,程序退出,请输入正整数");
}
}
}
热心网友
时间:2024-03-13 19:28
import java.util.Scanner;
public class ChengXu14 {
public static int method(int year,int mouth,int day){
int x;
if(year%4==0){
x=29;
}else{
x=28;
}
if(mouth==1){
return day;
}else if(mouth==2){
return 31+method(year,mouth-1,day);
}else if(mouth==3){
return x+method(year,mouth-1,day);
}else if(mouth==4){
return 31+method(year,mouth-1,day);
}else if(mouth==5){
return 30+method(year,mouth-1,day);
}else if(mouth==6){
return 31+method(year,mouth-1,day);
}else if(mouth==7){
return 30+method(year,mouth-1,day);
}else if(mouth==8){
return 31+method(year,mouth-1,day);
}else if(mouth==9){
return 31+method(year,mouth-1,day);
}else if(mouth==10){
return 30+method(year,mouth-1,day);
}else if(mouth==11){
return 31+method(year,mouth-1,day);
}else{
return 30+method(year,mouth-1,day);
}
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("请输入年:");
int x=scanner.nextInt();
System.out.print("请输入月:");
int y=x=scanner.nextInt();
System.out.print("请输入日:");
int z=scanner.nextInt();
int h=method(x,y,z);
System.out.println(x+"年的第"+h+"天");
}
}
热心网友
时间:2024-03-13 19:29
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
int day = 0, month = 0, year = 0;// 天数
boolean bool = true;// 一开始设为输入真确
do {
try {
System.out.println("input year:");
year = Integer.parseInt(sc.readLine());
System.out.println("input month:");
month = Integer.parseInt(sc.readLine());
if( (month>12 || month<1) || year <= 0){
System.out.print("输入的年份或者月份有问题\n");
bool = false;
}
} catch (NumberFormatException e) {
System.out.print("输入有误,你输入的是:" + e.getMessage().substring(e.getMessage().indexOf("\"")+1,e.getMessage().lastIndexOf("\"")) + "\n请重新输入");
bool = false;
} catch (IOException e) {
System.out.print("输入有误,你输入的是:" +e.getMessage().substring(e.getMessage().indexOf("\"")+1,e.getMessage().lastIndexOf("\"")) + "\n请重新输入");
bool = false;
}
} while (!bool);
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 2:
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
// 2月只有29天
day = 29;
} else {
day = 28;
}
break;
default:
day = 30;
}
System.out.println(year + "-" + month + "有:" + day + "天");
}
}
热心网友
时间:2024-03-13 19:29
这是我以前写滴一个计算从你出生到现在共活啦多少天滴程序,你可以参考一下!代码如下:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Test6 {
public static void main(String[] args) {
int year=0,month=0,day=0;
int tianshu;
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年");
year = scanner.nextInt();
System.out.print("请输入月");
month = scanner.nextInt();
System.out.print("请输入日");
day = scanner.nextInt();
tianshu=manyday(year , month , day );
System.out.println("您生活了:"+tianshu+"天");
}
private static int manyday(int year,int month,int day){
int shu = 0;
int todayyear,todaymonth,todayday;
String sNow = "";
int sheng=0;
Date today = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
sNow = sdf.format(today);
todayyear = Integer.parseInt(sNow.substring(0,4));
todaymonth = Integer.parseInt(sNow.substring(4,6));
todayday = Integer.parseInt(sNow.substring(6));
for(int i = year + 1;i < todayyear;i ++){
if (i % 4 == 0) {
shu = shu + 366;
}
else {
shu = shu + 365;
}
}
sheng=howday(year, month, day);
if(year % 4 == 0){
sheng = 366 - sheng ;
}else {
sheng = 365 - sheng;
}
shu = shu + sheng + howday(todayyear, todaymonth, todayday);
return shu;
}
private static int howday(int year,int month,int day){
int da[] = {1,3,5,7,8,10,12};
int xiao[] = {4,6,9,11};
int tian = 0;
if(month == 1){
tian = day;
}
else{
for(int i = 1;i < month;i++){
if(panan(da, i)){
tian = tian + 31;
}
else if(panan(xiao, i)){
tian = tian + 30;
}
else if ((i == 2)&&(year%4 == 0)) {
tian = tian + 29;
}else if ((i == 2)&&(year%4 != 0)) {
tian = tian + 28;
}
}
tian = tian + day;
}
return tian;
}
private static boolean panan(int[] a,int bianliang){
boolean boo = false;
for(int i = 0;i < a.length;i ++){
if(a[i] == bianliang){
boo = true;
break;
}
}
return boo;
}
}