Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class magicnumber {
- public static void main(String[] args) throws IOException{
- //gets input as a string
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("input an integer to play, input 'q' to quit");
- String in = null;
- try{
- do{
- in = br.readLine();
- //checks for exit case or empty line to avoid errors
- if(!(in.equals("q")||in.equals("")))
- //starts the game and outputs the magic number phrase when it's done
- System.out.println(playMagicNum(in));
- }while(!(in.equals("q")||in.equals("")));
- //loop ends here
- } catch(IOException ioe){
- System.out.println("IO error");
- System.exit(1);
- }
- System.out.println("thanks for playing!");
- }
- public static String playMagicNum(String input){
- String out = ""; //second number in strings
- String in = ""; //first number in strings
- int count = 0; //integer outputs of string lengths. or how many characters in each number
- String sTemp = ""; //stores each number converted to string
- in = input;
- //seed the loop with the first number
- sTemp = convert(Integer.parseInt(in));
- //will loop until the sTemp value starts with 'four' meaning the last word used was 'four'
- while(!(sTemp.equals("four"))){
- //counts the length of the first number, and passes the word to the output
- count = sTemp.length();
- in = sTemp;
- //converts the length of the first number into a word, then passes it to the output
- sTemp = convert(count);
- out = sTemp;
- //output the word-pairs
- System.out.println(in+" is "+out);
- }
- return "4 is the magic number";
- }
- /*
- * int-to-word conversion methods were found at http://stackoverflow.com/questions/3911966/how-to-convert-number-to-words-in-java
- */
- public static final String[] units = {
- "", "one", "two", "three", "four", "five", "six", "seven",
- "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
- "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
- };
- public static final String[] tens = {
- "", // 0
- "", // 1
- "twenty", // 2
- "thirty", // 3
- "forty", // 4
- "fifty", // 5
- "sixty", // 6
- "seventy", // 7
- "eighty", // 8
- "ninety" // 9
- };
- public static String convert(final int n) {
- if (n < 0) {
- return "minus " + convert(-n);
- }
- if (n < 20) {
- return units[n];
- }
- if (n < 100) {
- return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
- }
- if (n < 1000) {
- return units[n / 100] + " hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
- }
- if (n < 1000000) {
- return convert(n / 1000) + " thousand" + ((n % 1000 != 0) ? " " : "") + convert(n % 1000);
- }
- if (n < 1000000000) {
- return convert(n / 1000000) + " million" + ((n % 1000000 != 0) ? " " : "") + convert(n % 1000000);
- }
- return convert(n / 1000000000) + " billion" + ((n % 1000000000 != 0) ? " " : "") + convert(n % 1000000000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement