Advertisement
Guest User

magic number game solution

a guest
Sep 4th, 2014
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1.  
  2. import java.io.*;
  3.  
  4. public class magicnumber {
  5.  
  6.     public static void main(String[] args) throws IOException{
  7.        
  8.         //gets input as a string
  9.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  10.         System.out.println("input an integer to play, input 'q' to quit");
  11.         String in = null;
  12.         try{
  13.             do{
  14.                 in = br.readLine();
  15.                 //checks for exit case or empty line to avoid errors
  16.                 if(!(in.equals("q")||in.equals("")))
  17.                     //starts the game and outputs the magic number phrase when it's done
  18.                     System.out.println(playMagicNum(in));
  19.             }while(!(in.equals("q")||in.equals("")));
  20.             //loop ends here
  21.         } catch(IOException ioe){
  22.             System.out.println("IO error");
  23.              System.exit(1);
  24.         }
  25.        
  26.         System.out.println("thanks for playing!");
  27.        
  28.  
  29.     }
  30.    
  31.     public static String playMagicNum(String input){
  32.         String out = ""; //second number in strings
  33.         String in = ""; //first number in strings
  34.         int count = 0; //integer outputs of string lengths. or how many characters in each number
  35.         String sTemp = ""; //stores each number converted to string
  36.         in = input;
  37.         //seed the loop with the first number
  38.         sTemp = convert(Integer.parseInt(in));
  39.         //will loop until the sTemp value starts with 'four' meaning the last word used was 'four'
  40.         while(!(sTemp.equals("four"))){
  41.             //counts the length of the first number, and passes the word to the output
  42.             count = sTemp.length();
  43.             in = sTemp;
  44.             //converts the length of the first number into a word, then passes it to the output
  45.             sTemp = convert(count);
  46.             out = sTemp;
  47.             //output the word-pairs
  48.             System.out.println(in+" is "+out);
  49.         }
  50.         return "4 is the magic number";
  51.     }
  52.    
  53.     /*
  54.      * int-to-word conversion methods were found at http://stackoverflow.com/questions/3911966/how-to-convert-number-to-words-in-java
  55.      */
  56.    
  57.      public static final String[] units = {
  58.          "", "one", "two", "three", "four", "five", "six", "seven",
  59.          "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
  60.          "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
  61.       };
  62.  
  63.      public static final String[] tens = {
  64.              "",        // 0
  65.              "",        // 1
  66.              "twenty",  // 2
  67.              "thirty",  // 3
  68.              "forty",   // 4
  69.              "fifty",   // 5
  70.              "sixty",   // 6
  71.              "seventy", // 7
  72.              "eighty",  // 8
  73.              "ninety"   // 9
  74.      };
  75.    
  76.      public static String convert(final int n) {
  77.          if (n < 0) {
  78.              return "minus " + convert(-n);
  79.          }
  80.    
  81.          if (n < 20) {
  82.              return units[n];
  83.          }
  84.    
  85.          if (n < 100) {
  86.              return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10];
  87.          }
  88.    
  89.          if (n < 1000) {
  90.              return units[n / 100] + " hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
  91.          }
  92.    
  93.          if (n < 1000000) {
  94.              return convert(n / 1000) + " thousand" + ((n % 1000 != 0) ? " " : "") + convert(n % 1000);
  95.          }
  96.    
  97.          if (n < 1000000000) {
  98.              return convert(n / 1000000) + " million" + ((n % 1000000 != 0) ? " " : "") + convert(n % 1000000);
  99.          }
  100.    
  101.          return convert(n / 1000000000) + " billion"  + ((n % 1000000000 != 0) ? " " : "") + convert(n % 1000000000);
  102.      }
  103.    
  104.    
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement