I'm trying to write a code that validates that an integer is between 1 and 9. Then it prints out that integer as a word. User inputs 0 to exit program.
Example:
Enter an integer between 1 and 9
5
Integer entered, five
Enter an integer between 1 and 9
10
Integer is not beween 1 and 9
Enter an integer between 1 and 9
0
Goodbye
I am having an issue with the validating the integer is between 1 and 9 with if statements. As well as how to print out the integer as words. I am extremely new to java (started 6 weeks ago) so even this is way over my head, but here is what I have so far.
mport java.util.Scanner;
public class Validate {
//Engine that runs main program.
public static void main(String... Args){
Scanner input = new Scanner(System.in);
//Prompts user to enter an integer between 1 and 9.
System.out.println("Enter an integer between 1 and 9! " + " Enter 0 to exit application. ");
System.out.print(" What is you integer? ");
int userNumber = input.nextInt();
while (userNumber >= 0) {
// User inputs 0 to break the loop
if (userNumber == 0) {
System.out.print( "Thank you for playing! " + "Good bye! ");
break; }
else if (userNumber > 0 && < 9)
//then print out numbers as words using convertIntegerToWords(int numWord)
else if (userNumber < 0 || > 9)
System.out.print( " The integer you entered is not between 1 and 9")
}
}
public static String convertIntegerToWords(int numWord) {
String numWordString;
switch (numWord) {
case 1: numWordString = "One";
case 2: numWordString = "Two";
case 3: numWordString = "Three";
case 4: numWordString = "Four";
case 5: numWordString = "Five";
case 6: numWordString = "Six";
case 7: numWordString = "Seven";
case 8: numWordString = "Eight";
case 9: numWordString = "Nine";
}
return false;
}
}
Thanks for taking the time to look at this.