0

ランダムな Haiku を生成するプログラムを作成する必要があります。指定された数の音節名詞、動詞、および形容詞を含むファイルをプログラムに読み込ませることを計画していますが、コーディングに問題があります。現在、次のようになっています。

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)]);
        }
     }
}

私の出力では、「The」形容詞部分のみを取得しています。どうしてこれなの?

そうそう、私は今のところ最初の行を正しく印刷することに取り組んでいます。

4

2 に答える 2

4

最初の書式指定子がprintf()引数と一致しません:

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

これによりMissingFormatArgumentException、形容詞部分が出力された後、プログラムが途中で終了します。

于 2013-04-10T19:21:12.220 に答える
1

System.out.printf の 2 番目の引数を指定していないため、次の行でシンボル エラーが見つかりません。

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

2 番目の書式指定子を削除して、次のように記述します。

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

これで問題が解決するはずです。

于 2013-04-10T19:27:19.840 に答える