2016-11-12 38 views
-3

配列とユーザからの入力を使用してランダム詩生成プログラムを作成する必要があります(ユーザは最小3つの形容詞と3つの名詞を入力する必要があります)名詞と形容詞を2度再利用することはできません)、それを表示します。詩には3本の線が必要です。ここに私のコードはありますが、それは想定されている方法では全く機能しません!私が間違ったことを教えてください!ランダム詩生成器

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'); 

} 

}

+0

poemlineを試します。 poemline [1] = adj + noun2; poemline [2] = adj + noun2; adjとnoun2はどこで設定しますか? –

+0

どのように機能していますか? 「なぜ私のコードは機能していないのですか?」というのは、トピック外のスタイルの質問です。どのように動作しているか、何が起こるべきかなどを示すために質問を編集してください。 –

+0

彼らは次のようなものになっています:noun2 =(int)(Math.random()* 2);同じもののためのものです。 – Ray

答えて

0

[0] = ADJ + noun2この

import java.util.Random; 
import java.util.Scanner; 

public class Test { 
public static void main(String[] args) { 

    Scanner kb = new Scanner (System.in); 

    char ch; 

    do{ 

     int x,y; 
     String noun2 = null; 
     String adj = null; 


     //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(); 
     String[] noun = null; 
     String[] adjective = null; 



     if (x >= 3){ 
      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){ 
      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]; 
     Random rnd = new Random(); 

     poemline[0] = adjective[rnd.nextInt(y-1)] +" "+ noun[rnd.nextInt(x-1)]; 
     poemline[1] = adjective[rnd.nextInt(y-1)] +" "+ noun[rnd.nextInt(x-1)]; 
     poemline[2] = adjective[rnd.nextInt(y-1)] +" " +noun[rnd.nextInt(x-1)]; 

     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'); 

} 

}

+1

コードのみの回答は投稿しないでください。あなたが何を変えたのか、そしてOPが何を間違えたのかを説明してください – Tom