Advertisement
fosterbl

Shakespeare.java - Complete

Dec 20th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. public class Shakespeare{
  2.  
  3.    public static String[] regAlphabet = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "};
  4.    public static String[] shortAlphabet = {"t", "o", "b", "e", "r", "n", " "};
  5.    public static String[] freqAlphabet = {"t","o"," ","b","e"," ","o","r"," ","n","o","t"," ","t","o"," ","b","e"};
  6.    public static String goal = "to be or not to be";
  7.    
  8.    public static void main(String[] args){
  9.       while(true){
  10.          String phrase = randomPhrase( freqAlphabet );
  11.          int matches = numMatches( phrase );
  12.          if( matches > 14 ) System.out.println( matches + "==>" + phrase );
  13.       }
  14.    }
  15.    
  16.    public static String randomPhrase(String[] alphabet){
  17.       String result = "";
  18.       for( int i = 0; i < goal.length(); i++){
  19.          int r = (int)(Math.random() * alphabet.length);
  20.          result += alphabet[r];
  21.       }
  22.       return result;
  23.    }
  24.    
  25.    public static int numMatches( String phrase ){
  26.       int numMatches = 0;
  27.       for( int i = 0; i < phrase.length(); i++ ){
  28.          if( phrase.substring(i, i+1).equals( goal.substring(i,i+1) ) ) numMatches++;
  29.       }
  30.       return numMatches;
  31.    }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement