ie. 357 = December 23rd
Requirements:
• To get the number from the user, use JOptionPane.showInputDialog() as described in section 2.5 of the textbook (Chapter 2’s Graphics Supplement).
• Use an if-else structure to print the correct month name.
• Use a switch statement to print the ordinal indicator (st, nd, rd, or th).
• You may assume that you will only receive an integer number between 1 and 365.
what I have thus far..
package program1;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class Program1 extends JFrame {
private static final int LAST_DAY_OF_JANUARY = 31;
private static final int LAST_DAY_OF_FEBRUARY = LAST_DAY_OF_JANUARY + 28;
private static final int LAST_DAY_OF_MARCH = LAST_DAY_OF_FEBRUARY + 31;
private static final int LAST_DAY_OF_APRIL = LAST_DAY_OF_MARCH + 30;
private static final int LAST_DAY_OF_MAY = LAST_DAY_OF_APRIL + 31;
private static final int LAST_DAY_OF_JUNE = LAST_DAY_OF_MAY + 30;
private static final int LAST_DAY_OF_JULY = LAST_DAY_OF_JUNE + 31;
private static final int LAST_DAY_OF_AUGUST = LAST_DAY_OF_JULY + 31;
private static final int LAST_DAY_OF_SEPTEMBER = LAST_DAY_OF_AUGUST + 30;
private static final int LAST_DAY_OF_OCTOBER = LAST_DAY_OF_SEPTEMBER + 31;
private static final int LAST_DAY_OF_NOVEMBER = LAST_DAY_OF_OCTOBER + 30;
private static final int LAST_DAY_OF_DECEMBER = LAST_DAY_OF_NOVEMBER + 31;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
JOptionPane.showInputDialog("Enter the day of the year (1..365)");
int input = keyboard.nextInt();
String date;
if (input <= LAST_DAY_OF_JANUARY) {
date = "January";
} else if (input > LAST_DAY_OF_JANUARY && input < LAST_DAY_OF_MARCH) {
date = "February";
} else if (input > LAST_DAY_OF_MARCH && input < LAST_DAY_OF_APRIL) {
date = "March";
} else if (input > LAST_DAY_OF_APRIL && input < LAST_DAY_OF_MAY) {
date = "April";
} else if (input > LAST_DAY_OF_MAY && input < LAST_DAY_OF_JUNE) {
date = "May";
} else if (input > LAST_DAY_OF_JUNE && input < LAST_DAY_OF_JULY) {
date = "June";
} else if (input > LAST_DAY_OF_JULY && input < LAST_DAY_OF_AUGUST) {
date = "July";
} else if (input > LAST_DAY_OF_AUGUST && input < LAST_DAY_OF_SEPTEMBER) {
date = "August";
} else if (input > LAST_DAY_OF_SEPTEMBER && input < LAST_DAY_OF_OCTOBER) {
date = "September";
} else if (input > LAST_DAY_OF_OCTOBER && input < LAST_DAY_OF_NOVEMBER) {
date = "October";
} else if (input > LAST_DAY_OF_NOVEMBER && input < LAST_DAY_OF_DECEMBER) {
date = "November";
} else {
date = "December";
}
System.out.println(date);
}
}
I'm not sure why it wont choose a month and print it and also i'm not sure how to get the exact date number.

New Topic/Question
Reply



MultiQuote





|