java基礎語法練習--輸入年月日確定星期幾
阿新 • • 發佈:2019-01-03
基礎語法練習不能少
package test; import java.util.Scanner; public class aa{ public static void main (String[]args){ System.out.println("今年是哪年?"); Scanner reader = new Scanner(System.in); int x = reader.nextInt(); int a=0,b=0,c=0,d=0,i=1; c=x-1; a=365*c ; for(i=1;i<=c;i++) { //計算出x-1年的天數,考慮閏年 if ((i%4==0&i%100!=0)||i%400==0){ b=b+1; } a=a+b; } //System.out.println(a); System.out.println("今天是幾月?"); Scanner book = new Scanner (System.in); int q = book.nextInt(); int t=q-1; switch(t) { case 0:d=0;break; case 1:d=31;break; case 2:d=31+28;break; case 3: d=31+28+31;break; case 4:d=31+28+31+30;break; case 5: d=31+28+31+30+31;break; case 6: d=31+28+31+30+31+30;break; case 7: d=31+28+31+30+31+30+31;break; case 8: d=31+28+31+30+31+30+31+31;break; case 9: d=31+28+31+30+31+30+31+31+30;break; case 10: d=31+28+31+30+31+30+31+31+30+31;break; case 11: d=31+28+31+30+31+30+31+31+30+31+30;break; } a=a+d; //System.out.println(a); System.out.println("今天是幾號?"); Scanner look = new Scanner(System.in); int w = reader.nextInt(); a=a+w; //計算今年天數,考慮閏年情況 if (((x%4==0&x%100!=0)||x%400==0)&q==2) a=a+1; //System.out.println(a); int h; //判斷星期幾 h=a%7; if(h==0) { System.out.println("輸入的日期是星期天"); } else{ System.out.println ("輸入的日期是星期"+h); } } }