samedi 21 février 2015

If statement troubles 7 hours in

I am 4 weeks into Java class with 0 experience. I'm asked to create a program which asks use to input the numerical representation of a month example 1 would be january & also an input for the year and have the program output display the month how many days in the month and if it is a leap year. This is one of my many attempts shortened. What am I doing wrong it starts with january n matter the input.



package leapmonth;

import javax.swing.JOptionPane;

public class LeapMonth {
public static void main(String[] args) {

int Year;
int MonthNumber;

String MonthString;


JOptionPane.showMessageDialog(null, " HELLO, WELCOME TO LEAPMONTH ");
MonthString = JOptionPane.showInputDialog( " Please enter the numerical respresentation of the month");
MonthNumber = Integer.parseInt(MonthString);
MonthString = JOptionPane.showInputDialog( " Please enter the year");
Year = Integer.parseInt(MonthString);

if( MonthNumber == 1 && Year % 4 == 0 && Year % 100 != 0 || Year % 400 == 0)
{
JOptionPane.showMessageDialog(null,"The month is January. " + "January has 31 days in it" + " and is also a leap year ");
}
else if( MonthNumber == 1 && Year % 4 != 0 && Year % 100 == 0 || Year % 400 != 0)
{
JOptionPane.showMessageDialog(null,"The month is January. " + "January has 31 days in it" + " and it is not a leap year ");
}
else if( MonthNumber == 2 && Year % 4 == 0 && Year % 100 != 0 || Year % 400 ==0)
{
JOptionPane.showMessageDialog(null,"The month is February. " + "February has 29 days in it because this is a leap year ");
}
else if( MonthNumber == 2 && Year % 4 != 0 && Year % 100 == 0 || Year % 400 != 0)
{
JOptionPane.showMessageDialog(null,"The month is February. " + "February has 28 days in it because this is not a leap year ");
}
}
}

Aucun commentaire:

Enregistrer un commentaire