2016-11-12 688 views
-3

我需要创建一个随机诗生成器,同时使用数组和用户输入(用户需要输入最少3个形容词和3个名词),那么程序必须随机组合一个形容词与名词(名词和形容词不能重复使用两次)并显示它。诗中需要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 [0] =形容词+名2; poemline [1] = adj + noun2; poemline [2] = adj +名词2; 你在哪里设置adj和名词2? –

+0

*它是如何运作的? “为什么我的代码不能工作”是一个题外话式的问题。请编辑你的问题,以显示事情是如何工作的,应该发生什么等。 –

+0

他们应该是这样的:noun2 =(int)(Math.random()* 2);并且相同的形容词 – Ray

回答

0

尝试此

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