At least I think the solution to my problem will involve a loop. I'm new to java, so I'm sorry if I'm on the wrong board.
Basically, my code asks the user to enter a number from 1 - 12 and it then converts their number to the corresponding month, and displays it. If the enter 13 then the programs quits. My problem is, if the enter any number other than 1-13, my code displays an 'invalid entry' message and prompts them to try again. How do I enter the code to make the program loop back to the beginning and let them enter a new number if their first try was invalid?
Here's my code:
import java.util.Scanner; public class Months { //variables private int numberEntered; //constructor public Months(){ } //methods public void getNumberFromUser(){ Scanner input = new Scanner(System.in); System.out.print("Please enter the numeric value of a month (13 to quit): "); numberEntered = input.nextInt(); } public void displayMonth(){ if(numberEntered == 1) System.out.println("You have chosen January"); else if(numberEntered == 2) System.out.println("You have chosen February"); else if(numberEntered == 3) System.out.println("You have chosen March"); else if(numberEntered == 4) System.out.println("You have chosen April"); else if(numberEntered == 5) System.out.println("You have chosen May"); else if(numberEntered == 6) System.out.println("You have chosen June"); else if(numberEntered == 7) System.out.println("You have chosen July"); else if(numberEntered == 8) System.out.println("You have chosen August"); else if(numberEntered == 9) System.out.println("You have chosen September"); else if(numberEntered == 10) System.out.println("You have chosen October"); else if(numberEntered == 11) System.out.println("You have chosen November"); else if(numberEntered == 12) System.out.println("You have chosen December"); else if(numberEntered == 13) System.out.println("Thanks for playing. Goodbye!"); else System.out.println("Your entry is invalid. Please try again."); } public static void main(String[] args) { Months userEntry = new Months(); userEntry.getNumberFromUser(); userEntry.displayMonth(); } }
Any help is really appreciated.