2013-04-10 2 views
0

임의의 Haikus를 생성하는 프로그램을 작성해야합니다. 내 계획은 지정된 수의 음절 명사, 동사 및 형용사가 포함 된 파일로 프로그램을 읽는 것이지만 코딩에 문제가 있습니다. 지금은 다음과 같습니다 : 나는 단지 "는"형용사 부분을 얻고 내 출력을 위해Haiku Generator Java

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

은. 왜 이런거야?

오, 그래, 나는 그 순간에 정확하게 첫 줄을 인쇄하는 중이다.

+0

'gen'은 어디에 정의되어 있습니까? – gparyani

+0

임의 생성 = 새로운 임의(); – user2149738

답변

1

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

이렇게하면 문제가 해결됩니다.

4

printf()의 형식 지정자는 인수와 일치하지 않습니다이 형용사 부분이 인쇄 된 후 너무 빨리 프로그램을 종료하는 MissingFormatArgumentException 발생합니다

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

. 이 같은

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

두 번째 형식 지정자를 제거하고 쓰기 : 당신이 라인에서 심볼 오류를 찾을 수 없습니다 생성하는 것 System.out.printf에 대한 두 번째 인자가 지정하지 않았으므로

+0

어떻게 해결할 수 있습니까? – user2149738

+0

이 메서드는 서식을 지정하기 위해 2 개의 인수를 기다리고 있으며 목록에서 형용사 인 1을 전달합니다. '% s'을 제거하거나 형용사 다음에 다른 인수를 추가하십시오. –

+0

잘 던져 버렸습니다. 감사! – user2149738