Mon Nov 28, 2011 11:38 am
import java.util.Scanner;
public class ProjectTwoFrost {
// Main method
public static void main(String[] args) {
// Create a scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter a single digit integer
System.out.println("Enter a single digit integer" + "Enter 0 to exit");
System.out.print("Integer?");
int digit = input.nextInt();
int number;
while (digit >= 0) {
// Sentinal statement to exit loop
if (digit == 0) {
System.out.print( "Thank you for playing! " + "Good bye! ");
break; }
// Continue into loop
else if (digit != 0)
// Call public static boolean validateLength(digit) method to validate integer is only one digit
if(validateLength(digit) == true){
// Call public static String convertIntegerToWords(number) method to convert integer "digit" to word form
convertIntegerToWords(number);
// Output the word value for the integer entered
System.out.println ("Your number " + digit + " in words is " + number );
// Prompt user to enter another integer or press 0 to quit
// Ensures loop will be finite
System.out.print("Enter a number or press 0 to quit.");
digit = input.nextInt();
}
System.out.print("Enter a number or press 0 to quit.");
digit = input.nextInt();
}
}
// Method to validate length of integer
public static boolean validateLength(int digit) {
// Validate length of integer
if ((digit > 0) && (digit < 10))
return true;
else
System.out.println("The integer is too long");
return false;
}
// Method to convert integer into words using a switch statement
public static String convertIntegerToWords(int number) {
String numberString;
switch (number) {
case 1: numberString = "one";
break;
case 2: numberString = "two";
break;
case 3: numberString = "three";
break;
case 4: numberString = "four";
break;
case 5: numberString = "five";
break;
case 6: numberString = "six";
break;
case 7: numberString = "seven";
break;
case 8: numberString = "eight";
break;
case 9: numberString = "nine";
break;
}
return numberString;
}
}
Tue Nov 29, 2011 1:08 am
Fri Dec 02, 2011 8:07 am
Tue Jan 24, 2012 1:51 pm
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.