Java 使用数组的字符串的随机组合
我试图找出如何组合用户输入的2个随机字符串。这个程序有点像一个疯狂的Libs游戏,但它是用来创作一首诗的。我首先要求用户输入要使用的名词的数量,然后将它们存储到一个数组中,然后询问形容词的数量,这些形容词也存储在一个数组中 具体问题如下:Java 使用数组的字符串的随机组合,java,arrays,random,Java,Arrays,Random,我试图找出如何组合用户输入的2个随机字符串。这个程序有点像一个疯狂的Libs游戏,但它是用来创作一首诗的。我首先要求用户输入要使用的名词的数量,然后将它们存储到一个数组中,然后询问形容词的数量,这些形容词也存储在一个数组中 具体问题如下: import java.util.Scanner; public class A3Question1 { public static void main(String[] args) { Scanner keyboard = new Scanne
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();
}
通过随机选择名词和形容词的组合来生成你的诗。
在分别使用用户提供的所有名词和形容词之前,不允许再次选择名词或形容词
现在我被要求通过组合输入的名词和形容词来生成一首诗,但是生成器需要是随机的。我该怎么做?到目前为止,我的计划如下:
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();
}
import java.util.Scanner;
公开课问题1
{
公共静态void main(字符串[]args)
{
扫描仪键盘=新扫描仪(System.in);
布尔环1=假;
布尔环2=假;
布尔环3=假;
int numNouns=0,numAdjectives=0;
字符串[]名词=新字符串[numNouns];
String[]形容词=新字符串[numAdjectives];
System.out.println(“--------------------------------------------------------”;
System.out.println(“让我们写首诗吧!”);
System.out.println(“--------------------------------------------------------”;
System.out.println();
而(!loop1)
{
System.out.print(“多少个名词?(最少3个):”;
numNouns=keyboard.nextInt();
如果(单位<3)
{
继续;
}
其他的
{
loop1=真;
}
System.out.println(“输入”+numNouns+”名词:);
名词=新字符串[numNouns];
对于(int i=0;i
}
样本输出:
编辑**:我清理了代码并将数组随机组合在一起。但是,生成的随机数组不能再次重用,我正在试图找出如何避免这个问题。我还需要将这首诗适当缩进,如图所示。例如,第一个输出没有空格,第二个输出有一个制表符,第三个输出有两个制表符。使用
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;
}
公共字符串(字符串[]名词,字符串[]形容词){
随机=新随机();
ArrayList nownList=新的ArrayList(Arrays.asList(nowns));
ArrayList adjList=新的ArrayList(Arrays.asList(形容词));
字符串值=”;
if(random.nextBoolean()){
int index=random.nextInt(nounomlist.size());
value+=名词列表。删除(索引);
}否则{
int index=random.nextInt(adjList.size());
value+=adjList.remove(索引);
}
返回值;
}
使用随机
对象在数组中选择随机索引。第二个随机变量可以选择是否从中选取名词或形容词数组
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;
}
公共字符串(字符串[]名词,字符串[]形容词){
随机=新随机();
ArrayList nownList=新的ArrayList(Arrays.asList(nowns));
ArrayList adjList=新的ArrayList(Arrays.asList(形容词));
字符串值=”;
if(random.nextBoolean()){
int index=random.nextInt(nounomlist.size());
value+=名词列表。删除(索引);
}否则{
int index=random.nextInt(adjList.size());
value+=adjList.remove(索引);
}
返回值;
}
您必须将阵列随机化。一个简单的方法是拾取数组中的任意2个元素并相互交换。这样做足够多的时间,你将有一个随机数组
这是一个样品
package com.example;
import java.util.Random;
public class RandomTest {
public static void main(String[] args) {
String[] nouns = {"roses", "tulips", "grass"};
shuffle(nouns, 50);
String[] adjectives = {"red", "lovely", "gorgeous", "green"};
shuffle(adjectives, 50);
for(int i = 0; i < nouns.length && i < adjectives.length; i++) {
System.out.println(adjectives[i] + " " + nouns[i]);
}
}
public static void shuffle(String[] list, int swapCount) {
Random random = new Random();
for(int i = 0; i < swapCount; i++) {
int index1 = random.nextInt(list.length);
int index2 = random.nextInt(list.length);
//Swap these two with each other
String temp = list[index1];
list[index1] = list[index2];
list[index2] = temp;
}
}
}
您必须将阵列随机化。一个简单的方法是拾取数组中的任意2个元素并相互交换。这样做足够多的时间,你将有一个随机数组 这是一个样品
package com.example;
import java.util.Random;
public class RandomTest {
public static void main(String[] args) {
String[] nouns = {"roses", "tulips", "grass"};
shuffle(nouns, 50);
String[] adjectives = {"red", "lovely", "gorgeous", "green"};
shuffle(adjectives, 50);
for(int i = 0; i < nouns.length && i < adjectives.length; i++) {
System.out.println(adjectives[i] + " " + nouns[i]);
}
}
public static void shuffle(String[] list, int swapCount) {
Random random = new Random();
for(int i = 0; i < swapCount; i++) {
int index1 = random.nextInt(list.length);
int index2 = random.nextInt(list.length);
//Swap these two with each other
String temp = list[index1];
list[index1] = list[index2];
list[index2] = temp;
}
}
}
你说的组合是指连接?从某种意义上说,是的。这是一个需要的示例输出,后编辑@Jake0991您能在您的描述中添加一个示例吗?通过组合,您的意思是连接?从某种意义上说,是的。以下是所需的示例输出po