samedi 12 novembre 2016

Random poem generator

I need to create a random poem generator while using arrays and input from the user (the user needs to input a minimal of 3 adjectives and 3 nouns) then the program has to randomly combine an adjective with a noun (a noun and an adjective can not be re-used twice) and display it. There needs to be 3 lines in the poem. Here's my code but it doesn't function at all the way it's supposed to! Please tell me what i did wrong!

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner kb = new Scanner (System.in);

    char ch;

    do{

    int x,y;
    String noun2, adj;


    //The following is my "Welcome"/Opening message to the user
    System.out.println("     ---------------------------------------");
    System.out.println("     Welcome to the random poem generator!!!");
    System.out.println("     ---------------------------------------\n\n");

    System.out.println("Please enter a set of relevant nouns and adjectives that are inspired by the themes of nature and wilderness! \n\n");

    //The following will work as a prompt message for the user to input a value when demanded
    System.out.println("Number of nouns you prefer (minimum 3): ");
    x = kb.nextInt();
    System.out.println("Number of adjectives you prefer (minimum 3): ");
    y = kb.nextInt();



    if (x >= 3){

        String[] noun = new String [x];
        for(int j=0; j<noun.length;j++){
            System.out.println("Enter your " + x + " nouns: ");
            System.out.println(j);
            noun[j] = kb.nextLine();
        }

    }
    else{
        System.out.println("Number of nouns you prefer (minimum 3): ");
        x = kb.nextInt();
    }

    if (y >=3){
        String[] adjective = new String [y];
        for(int j=0; j<adjective.length;j++){
                System.out.println("Enter your " + y + " adjectives: ");
                System.out.println(j);
                adjective[j] = kb.nextLine();   
        }

    }
    else{
        System.out.println("Number of adjectives you prefer (minimum 3): ");
        y = kb.nextInt();
    }

    System.out.println("     --------------------");
    System.out.println("     Here is your poem!!!");
    System.out.println("     --------------------\n\n");


    String[] poemline = new String[3];
    poemline[0] = adj + noun2;
    poemline[1] = adj + noun2;
    poemline[2] = adj + noun2;

    System.out.println(poemline[0]);
    System.out.println("/t/t" + poemline[1]);
    System.out.println("/t/t/t/t" + poemline[2]);
    System.out.println("Would you like to try another poem (y/n)? ");
    String answer;
    answer = kb.nextLine().trim().toUpperCase();
    ch = answer.charAt(0);
    }while(ch == 'Y');

}

}




Aucun commentaire:

Enregistrer un commentaire