Java calendar not working correctly -
my task make calendar based on user's input first day. if year eg. 2 = tuesday , year can figure out if leap year. managed working except following case:
when input 2013 2(meaning january 1st should tuesday), places first of january on saturday. can check if dates showing correctly going calendar on computer , comparing. have looked through code cannot figure out how 2 entered in start results in dates starting on saturday.
where going wrong?
package javaapplication6; import java.util.scanner; import javax.swing.joptionpane; public class javaapplication6 { public static void main(string args[]){ scanner input = new scanner(system.in); system.out.println("enter year , day number eg. 2034 2: "); int year = input.nextint(); int day = input.nextint(); day -= 1; boolean leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; for(int = 1; <= 12; i++){ string wday = ""; string wmonth = ""; int days = 0; int current = 0; switch(i){ case 1: days = 31; day+= 31; wmonth = "january"; break; case 2: if(leap){days = 29;}else{days = 28;}; if(leap){day += 29;}else{day += 28;}; wmonth = "february"; break; case 3: days = 31; day+= 31; wmonth = "march"; break; case 4: days = 30; day+= 30; wmonth = "april"; break; case 5: days = 31; day+= 31; wmonth = "may"; break; case 6: days = 30; day+= 30; wmonth = "june"; break; case 7: days = 31; day+= 31; wmonth = "july"; break; case 8: days = 31; day+= 31; wmonth = "august"; break; case 9: days = 30; day+= 30; wmonth = "september"; break; case 10: days = 31; day+= 31; wmonth = "october"; break; case 11: days = 30; day+= 30; wmonth = "november"; break; case 12: days = 31; wmonth = "december"; break; } system.out.printf(" %15s\n", wmonth); system.out.println(" ___________________________"); system.out.printf(" %3s %3s %3s %3s %3s %3s %3s\n", "mon", "tue", "wed", "thu", "fri", "sat", "sun"); for(int space = 0; space <= (day % 7) + 1; space++){ system.out.print(" "); current ++; } for(int date = 1; date <= days; date++){ if(current > 7){ system.out.print("\n "); current = 1; } if(date < 10){ system.out.print(date + " "); }else{ system.out.print(date + " "); } current++; } system.out.println("\n"); } } }
are sure worked? run 2014 , 3 , started sunday. i've made changes, , think works should:
switch(i){ case 1: days = 31; day+= 0; wmonth = "january"; break; case 2: if(leap){days = 29;}else{days = 28;}; day += 31; wmonth = "february"; break; case 3: days = 31; if(leap){day += 29;}else{day += 28;}; wmonth = "march"; break; case 4: days = 30; day+= 31; wmonth = "april"; break; case 5: days = 31; day+= 30; wmonth = "may"; break; case 6: days = 30; day+= 31; wmonth = "june"; break; case 7: days = 31; day+= 30; wmonth = "july"; break; case 8: days = 31; day+= 31; wmonth = "august"; break; case 9: days = 30; day+= 31; wmonth = "september"; break; case 10: days = 31; day+= 30; wmonth = "october"; break; case 11: days = 30; day+= 31; wmonth = "november"; break; case 12: days = 31; day+= 30; wmonth = "december"; break; }
and
for(int space = 0; space <= (day % 7); space++){ //here u added 1 space much.
in switch statement added days quickly. , in first month had day=31 should have had number of days input - 1.
Comments
Post a Comment