使用年份输入和第一天输入获取一个月中的第一天

Get first days in a month with year input and first day input

到目前为止,这是我的代码。我想我做错了。 帮助将不胜感激:

输入的年份是 2013 年; 天的输入是 2(表示星期二)

public class firstMonthDay {

  public static void main(String[] args) {

    Scanner input= new Scanner(System.in);

    System.out.print("Enter Year:");        
    int year = input.nextInt();

    System.out.print("Enter day of the week:");        
    int inputDay = input.nextInt();

    int firstday=0;        
    int daysInMonth=0;        
    int month =0;

    for (int i=1; i<=365; i++){

      switch(daysInMonth){

      case 1: daysInMonth += 31 ;
      case 2: daysInMonth += 28  ;
      case 3: daysInMonth += 31  ;  
      case 4: daysInMonth += 30  ;  
      case 5: daysInMonth += 31  ;  
      case 6: daysInMonth += 30  ;  
      case 7: daysInMonth += 31  ;  
      case 8: daysInMonth += 31  ;  
      case 9: daysInMonth += 30  ;  
      case 10: daysInMonth += 31 ;  
      case 11: daysInMonth += 30 ;  
      case 12: daysInMonth += 31 ;
        break;
      default:


        switch( firstday=daysInMonth-inputDay%7){
        case 1:System.out.print("Monday");break;
        case 2:System.out.print("Tuesday");break;
        case 3:System.out.print("Wednesday");break;
        case 4:System.out.print("Thursday");break;
        case 5:System.out.print("Friday");break;
        case 6:System.out.print("Saturday");break;
        case 7:System.out.print("Sunday");break;
        default:

          while (month<12){ 
            month++;
            switch(month){
            case 1: System.out.println("January");break;
            case 2: System.out.println("Febuary");break;
            case 3: System.out.println("March"); break;
            case 4: System.out.println("April");break;
            case 5: System.out.println("May");break;
            case 6: System.out.println("June");break;
            case 7: System.out.println("july");break;
            case 8: System.out.println("August");break;
            case 9: System.out.println("September");break;
            case 10: System.out.println("October");break;
            case 11: System.out.println("November");break;
            case 12: System.out.println("December");break;
            default:

            }
          }
        }
      }
      System.out.println("the first day of"+month+"is:"+firstday);
    }
  }      
}

结果应该是

"the first day of january is tuesday"
......
"the first day of december is Sunday"

你的问题我不太清楚。 您可以使用以下代码并输入日期将为您提供日期。

   String input_date="23/03/2015";
   SimpleDateFormat format1=new SimpleDateFormat("dd/MM/yyyy");
   Date dt1=format1.parse(input_date);
   DateFormat format2=new SimpleDateFormat("EEEE"); 
   String finalDay=format2.format(dt1);
   System.out.println(finalDay);

另外,这个

"the first day of january is tuesday"

永远不会发生,因为在

System.out.println("the first day of"+month+"is:"+firstday);

第一天被声明为一个整数。

此外,请简要说明您要完成的任务。

没有必要从头开始写这个。使用在 Java 8.

中添加的 java.time.Monthjava.time.LocalDate
import java.time.Month;
import java.util.Scanner;
import java.time.LocalDate;

public class firstMonthDay {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Year:");
    int year = input.nextInt();
    input.close();

    Month month = Month.JANUARY;
    do {
      System.out.println("The first day of " + month + " is " + LocalDate.of(year, month, 1).getDayOfWeek());
      month = month.plus(1);
    }
    while (!month.equals(Month.JANUARY));
  }
}

输出为:

Enter Year:2015
The first day of JANUARY is THURSDAY
The first day of FEBRUARY is SUNDAY
The first day of MARCH is SUNDAY
The first day of APRIL is WEDNESDAY
The first day of MAY is FRIDAY
The first day of JUNE is MONDAY
The first day of JULY is WEDNESDAY
The first day of AUGUST is SATURDAY
The first day of SEPTEMBER is TUESDAY
The first day of OCTOBER is THURSDAY
The first day of NOVEMBER is SUNDAY
The first day of DECEMBER is TUESDAY

Calendar是你的朋友。

Scanner input= new Scanner(System.in);
System.out.print("Enter Year:");
int year = input.nextInt();
Calendar cal = Calendar.getInstance();
for (int i=0; i<12; i++) {
    cal.set(year, i, 1);
    System.out.printf( "the first day of %s is %s\n",
            cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()),
            cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault()) );
}//end for i