import java.util.Scanner; public class Assignment { public static void main(Stri
ID: 3537528 • Letter: I
Question
import java.util.Scanner;
public class Assignment {
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
int month, year;
System.out.print("Please enter month in integer form.");
int number = keyBoard.nextInt();
System.out.print("Please enter year in integer form.");
int number = keyBoard.nextInt();
if ( month == 1 ) {System.out.println("January " + year + " has 31 days.");
}else if ( month == 2 ) {if (year%4 == 0 )
System.out.println("February" + year + "has 29 days");
else System.out.println("February" +year + "has 28 days");
} else if ( month == 3 ) {System.out.println("March" + year + "has 31 days.");
} else if ( month == 4 ) {System.out.println("April" + year + "has 30 days.");
} else if ( month == 5 ) {System.out.println("May" + year + "has 31 days.");
} else if ( month == 6 ) {System.out.println("June " + year + " has 30 days.");
} else if ( month == 7 ) {System.out.println ("July" + year + "has 31 days.");
} else if ( month == 8 ) {System.out.println ("August" + year + "has 31 days.");
} else if ( month == 9 ) {System.out.println("September" + year + "has 30 days.");
} else if ( month == 10 ) {System.out.println ("October" + year + "has 31 days.");
} else if ( month == 11 ) {System.out.println ("November" + year + "has 30 days.");
} else if ( month == 12 ) {System.out.println ("December" + year + "has 31 days.");
} else {System.out.println("Invalid choice.");
}
}
}
Explanation / Answer
On compiling the following error comes
Unresolved compilation problem: Duplicate local variable number
Error Reason : duplicate variable number
..............................................................................................................................................
Corrected Code
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
int month, year;
System.out.print("Please enter month in integer form.");
month = keyBoard.nextInt();
System.out.print("Please enter year in integer form.");
year = keyBoard.nextInt();
if ( month == 1 ) {System.out.println("January " + year + " has 31 days.");
}else if ( month == 2 ) {if (year%4 == 0 )
System.out.println("February" + year + "has 29 days");
else System.out.println("February" +year + "has 28 days");
} else if ( month == 3 ) {System.out.println("March" + year + "has 31 days.");
} else if ( month == 4 ) {System.out.println("April" + year + "has 30 days.");
} else if ( month == 5 ) {System.out.println("May" + year + "has 31 days.");
} else if ( month == 6 ) {System.out.println("June " + year + " has 30 days.");
} else if ( month == 7 ) {System.out.println ("July" + year + "has 31 days.");
} else if ( month == 8 ) {System.out.println ("August" + year + "has 31 days.");
} else if ( month == 9 ) {System.out.println("September" + year + "has 30 days.");
} else if ( month == 10 ) {System.out.println ("October" + year + "has 31 days.");
} else if ( month == 11 ) {System.out.println ("November" + year + "has 30 days.");
} else if ( month == 12 ) {System.out.println ("December" + year + "has 31 days.");
} else {System.out.println("Invalid choice.");
}
}
}
....................................................................................................................
Output
Please enter month in integer form.06
Please enter year in integer form.2013
June 2013 has 30 days.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.