Skip to content

Instantly share code, notes, and snippets.

@ethan-gallant
Created March 22, 2018 18:22
Show Gist options
  • Save ethan-gallant/39391b3762d54597fd50cd03643fd7ec to your computer and use it in GitHub Desktop.
Save ethan-gallant/39391b3762d54597fd50cd03643fd7ec to your computer and use it in GitHub Desktop.
package com.exclnetworks.wordprograms;
import java.util.Scanner;
/*
* Created by Ethan Joshua Gallant
* 2/12/2018
* Make random 6 letter words with exactly one or two vowels in them.
*/
public class RandomWord {
static char[] vowels = new char[] {'a','e','i','o','u'};
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("HOW MANY WORDS WOULD YOU LIKE CREATED");
int wordCount = input.nextInt();
//for each word the user wants created
for(int i = 0; i <= wordCount; i++)
{
System.out.println("#" + i + ". " + makeWord());
}
}
static String makeWord(){
//grab either one or two vowels
int vowelCount = (int) (Math.random() * 2) + 1;
String word = "";
//loop to create random letters that arent vowels
for(int i = 0; i < 6 - vowelCount; i++)
{
while(true)
{
char letter = (char) ('a'+(int) (Math.random() * 26));
//if the letter isnt a vowel: add to the word and then break.
if(!isVowel(letter))
{
word += letter;
break;
}
}
}
//add our vowels in random places
while(word.length() < 6)
{
int index = (int) (Math.random() * 5);
char vowel = vowels[(int) (Math.random() * 5)];
//split the string and shove our vowel in at a random position
word = word.substring(0,index) + vowel + word.substring(index, word.length());
}
return word;
}
static boolean isVowel(char c) {
for(char v: vowels)
{
if(c == v)
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment