gpt4 book ai didi

java - 使用数组随机组合字符串

转载 作者:太空宇宙 更新时间:2023-11-04 12:06:27 26 4
gpt4 key购买 nike

我试图弄清楚如何组合用户输入的 2 个随机字符串。这个程序有点像 Mad Libs 游戏,但它是用来创作一首诗的。我首先要求用户输入要使用的名词数量,然后将它们存储到一个数组中,然后询问形容词的数量,这些形容词也存储在一个数组中。

具体要求如下:

通过随机选择名词和形容词的组合来生成你的诗。在您分别使用用户提供的所有名词和形容词组之前,您不能再次选择名词或形容词。

现在我被要求通过组合输入的名词和形容词来生成一首诗,但生成器需要是随机的。我该怎么做?到目前为止我的程序如下:

import java.util.Scanner;
public class A3Question1
{

public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
boolean loop1 = false;
boolean loop2 = false;
boolean loop3 = false;
int numNouns = 0, numAdjectives = 0;
String[] nouns = new String[numNouns];
String[] adjectives = new String[numAdjectives];

System.out.println("-----------------------------------------");
System.out.println(" Let's write a poem! ");
System.out.println("-----------------------------------------");
System.out.println();

while (!loop1)
{
System.out.print("How many nouns? (min 3): ");
numNouns = keyboard.nextInt();

if (numNouns < 3)
{
continue;
}
else
{
loop1 = true;
}

System.out.println("Enter " + numNouns + " nouns: ");
nouns = new String[numNouns];

for (int i = 0; i < numNouns; i++)
{
nouns[i] = keyboard.next();
}
}
while (!loop2)
{
System.out.print("How many adjectives? (min 3): ");
numAdjectives = keyboard.nextInt();

if (numAdjectives < 3)
{
continue;
}
else
{
loop2 = true;
}

System.out.println("Enter " + numAdjectives + " adjectives: ");
adjectives = new String[numAdjectives];

for (int j = 0; j < numAdjectives; j++)
{
adjectives[j] = keyboard.next();
}

while (!loop3)
{
System.out.println("\n-----------------------------------");
System.out.println(" Here is my Java Poem! ");
System.out.println(" **LOOK AROUND** ");
System.out.println("-----------------------------------");
System.out.println();

for (int i = 0; i < numNouns && i < numAdjectives; i++)
{
int num1 = (int) (Math.random() * numNouns);
int num2 = (int) (Math.random() * numAdjectives);
System.out.println(nouns[num1] + adjectives[num2]);
}

System.out.println("\nAnother poem? (y/n): ");
String again = keyboard.next();

if (again.charAt(0) == 121 || again.charAt(0) == 89)
{
continue;
}
else
{
loop3 = true;
}
}

System.out.println("Thank you for using POEM GENERATOR! Have a good day!");
}
keyboard.close();
}

}

示例输出:

https://postimg.org/image/6ogwggw9f/

编辑**:我清理了代码并将数组随机组合在一起。但是,生成的随机数组无法再次重复使用,我正在尝试找出如何避免此问题。我还需要正确缩进这首诗,如图所示。例如,第一个输出没有空格,第二个输出有一个制表符,第三个输出有两个制表符。

最佳答案

使用Random对象在数组中选择随机索引。第二个随机数可以选择是否应从中选取名词或形容词数组。

public String randomPoem(String[] nouns, String[] adjectives) {
Random random = new Random();
ArrayList<String> nounList = new ArrayList<>(Arrays.asList(nouns));
ArrayList<String> adjList = new ArrayList<>(Arrays.asList(adjectives));
String value = "";
if (random.nextBoolean()) {
int index = random.nextInt(nounList.size());
value += nounList.remove(index);
} else {
int index = random.nextInt(adjList.size());
value += adjList.remove(index);
}
return value;
}

关于java - 使用数组随机组合字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40315578/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com