Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Shakespeare{
- 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", " "};
- public static String[] shortAlphabet = {"t", "o", "b", "e", "r", "n", " "};
- public static String[] freqAlphabet = {"t","o"," ","b","e"," ","o","r"," ","n","o","t"," ","t","o"," ","b","e"};
- public static String goal = "to be or not to be";
- public static void main(String[] args){
- while(true){
- String phrase = randomPhrase( freqAlphabet );
- int matches = numMatches( phrase );
- if( matches > 14 ) System.out.println( matches + "==>" + phrase );
- }
- }
- public static String randomPhrase(String[] alphabet){
- String result = "";
- for( int i = 0; i < goal.length(); i++){
- int r = (int)(Math.random() * alphabet.length);
- result += alphabet[r];
- }
- return result;
- }
- public static int numMatches( String phrase ){
- int numMatches = 0;
- for( int i = 0; i < phrase.length(); i++ ){
- if( phrase.substring(i, i+1).equals( goal.substring(i,i+1) ) ) numMatches++;
- }
- return numMatches;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement