首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Haiku生成器Java

Haiku生成器Java
EN

Stack Overflow用户
提问于 2013-04-11 03:16:42
回答 2查看 923关注 0票数 0

我需要写一个程序来生成随机的Haikus。我的计划是让程序读取包含指定数量的音节名词、动词和形容词的文件,但我在编码方面遇到了问题。现在它看起来是这样的:

代码语言:javascript
复制
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“形容词部分。为什么会这样呢?

哦,是的,我目前只致力于让第一行正确打印出来。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-11 03:27:20

由于您没有为System.out.printf指定第二个参数,因此它将生成无法在第1行找到符号错误:

代码语言:javascript
复制
System.out.printf("The %s %s \n",adjectiveList[gen.nextInt(adjectiveList.length)]);
                          ^

删除第二个格式说明符,并编写如下代码:

代码语言:javascript
复制
System.out.printf("The %s\n",       adjectiveList[gen.nextInt(adjectiveList.length)]);

这应该可以解决您的问题。

票数 1
EN

Stack Overflow用户

发布于 2013-04-11 03:21:12

第一个printf()的格式说明符与参数不匹配:

代码语言:javascript
复制
System.out.printf("The %s %s \n", adjectiveList[gen.nextInt(adjectiveList.length)]);

这将抛出一个MissingFormatArgumentException,在打印形容词部分之后提前结束您的程序。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15934501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档