vendredi 28 octobre 2016

Random combination of strings using arrays

I am trying to figure out how to combine 2 random Strings entered by the user. This program is kind of like a Mad Libs game but instead it is for creating a poem. I first start by asking the user to enter the number of nouns willing to use, and then storing them into an array, and follow by asking the amount of adjectives, which also are stored in an array.

Precisely what is asked is as follows:

Generate your poem by choosing at random combinations of noun and adjectives. You are not allowed to pick a noun or an adjective again until you have used all sets of noun and adjectives that the user has provided, respectively.

Now I am asked to generate a poem by combining the entered nouns and adjectives, but the generator needs to be random. How would I do this? So far my program is as folows:

import java.util.Scanner; public class A3Question1 {

public static void main(String[] args) 
{
    Scanner keyboard = new Scanner(System.in);

    System.out.println("-----------------------------------------");
    System.out.println("               Poem game                 ");
    System.out.println("-----------------------------------------");
    System.out.println();
    String[] nouns, adjectives;
    boolean loop1 = false;

    while (!loop1)
    {

        System.out.print("How many nouns (min 3): ");
        int numNouns = keyboard.nextInt();

        if (numNouns < 3)
        {
            continue;
        }

        nouns = new String[numNouns];

        System.out.println("Enter " + numNouns + " nouns");
        boolean loop2 = false;

        while (!loop2)
        {
            nouns[numNouns - 1] = keyboard.next();
            numNouns--;

            if (numNouns == 0)
            {
                loop2 = true;
            }
        }
        boolean loop3 = false;

        System.out.println("How many adjectives (min 3): ");
        int numAdjectives = keyboard.nextInt();

        while (!loop3)
        {
            if (numAdjectives < 3)
            {
                System.out.print("How many adjectives (min 3): ");
                numAdjectives = keyboard.nextInt();
            }
            else
            {
                loop3 = true;
            }
        }

        adjectives = new String[numAdjectives];

        System.out.println("Enter " + numAdjectives + " adjectives");
        boolean loop4 = false;

        while (!loop4)
        {
            adjectives[numAdjectives - 1] = keyboard.next();
            numAdjectives--;

            if (numAdjectives == 0)
            {
                loop4 = true;
            }
        }

    }
    keyboard.close();
}

}




Aucun commentaire:

Enregistrer un commentaire