我需要创建一个随机诗歌生成器,同时使用来自用户的数组和输入(用户需要输入最少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 :(得分: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');
}
}