簡體   English   中英

使用數組隨機組合字符串

[英]Random combination of strings using arrays

我正在試圖弄清楚如何組合用戶輸入的2個隨機字符串。 這個節目有點像瘋狂的自由游戲,但它是用來創作一首詩。 我首先要求用戶輸入要使用的名詞數量,然后將它們存儲到數組中,然后詢問形容詞的數量,這些形容詞也存儲在數組中。

確切地說,問題如下:

通過選擇名詞和形容詞的隨機組合來產生你的詩。 在分別使用用戶提供的所有名詞和形容詞組之前,不允許再次選擇名詞或形容詞。

現在我被要求通過組合輸入的名詞和形容詞來生成一首詩,但是生成器需要是隨機的。 我該怎么做? 到目前為止,我的計划如下:

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;
}

您必須將數組隨機化。 一個簡單的方法是選擇數組中的任何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;
        }
    }
}

連續運行輸出

lovely tulips
gorgeous roses
red grass

lovely grass
gorgeous tulips
green roses

lovely tulips
red roses
green grass

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM