Générateur de Haiku Java


J'ai besoin d'écrire un programme qui génère des haïkus aléatoires. Mon plan est de faire lire le programme dans des fichiers contenant un nombre spécifié de noms de syllabes, de verbes et d'adjectifs, mais j'ai un problème avec le codage. En ce moment, cela ressemble à ceci:

package poetryproject;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;



public class PoetryProject {

    public static void main(String[] args) throws FileNotFoundException {


        Random gen = new Random();

        Scanner adjectivesFile = new Scanner(new File("AdjectivesFile.dat"));
        Scanner nounFile = new Scanner(new File("NounFile.dat"));
        Scanner verbFile = new Scanner(new File("VerbFile.dat"));

        int adjectiveCount = adjectivesFile.nextInt();
        String[] adjectiveList = new String[adjectiveCount];
        for (int i = 0; i < adjectiveCount; i++) {
            adjectiveList[i] = adjectivesFile.nextLine();
        }
        adjectivesFile.close();

        int nounCount = nounFile.nextInt();
        String[] nounList = new String[nounCount];
        for (int i = 0; i < nounCount; i++) {
            nounList[i] = nounFile.nextLine();
        }
        nounFile.close();

        int verbCount = verbFile.nextInt();
        String[] verbList = new String[verbCount];
        for (int i = 0; i < verbCount; i++) {
            verbList[i] = verbFile.nextLine();
        }
        verbFile.close();

        for (int count = 1; count <= 1; count++) {
           System.out.printf("The %s %s \n",       adjectiveList[gen.nextInt(adjectiveList.length)]);
        }
        for (int count = 1; count <= 1; count++) {
            System.out.printf("%s %s \n", nounList[gen.nextInt(nounList.length)]);
        }
        for (int count = 1; count <= 1; count++) {
            System.out.printf("%s %s \n", verbList[gen.nextInt(verbList.length)]);
        }
     }
}

Pour ma sortie, je n'obtiens que la partie adjective "The". Pourquoi est-ce?

Oh oui, et je ne travaille que sur l'impression correcte de la première ligne pour le moment.

Author: user2149738, 2013-04-10

2 answers

Puisque vous n'avez pas spécifié un deuxième argument pour le Système.hors.printf il générerait impossible de trouver l'erreur de symbole à la ligne:

System.out.printf("The %s %s \n",adjectiveList[gen.nextInt(adjectiveList.length)]);
                          ^

Supprimer le deuxième spécificateur de format et l'écrire comme:

System.out.printf("The %s\n",       adjectiveList[gen.nextInt(adjectiveList.length)]);

Cela devrait résoudre votre problème.

 1
Author: Sohaib, 2013-04-10 19:27:19

Le spécificateur de format du premier {[1] }ne correspond pas aux arguments:

System.out.printf("The %s %s \n", adjectiveList[gen.nextInt(adjectiveList.length)]);

Cela lancera un MissingFormatArgumentException, terminer prématurément votre programme après l'impression de la partie adjective.

 4
Author: NPE, 2013-04-10 19:21:12