随机生成字符串

时间:2017-05-09 01:52:17

标签: java string random

我想知道是否有办法随机打印字符串。我有这个代码,我有#34;()"和#34;代表的岩石。"。现在我想要根据不同类中的随机int打印出来。然而,他们只是一条直线打印。我想知道是否有一种简单的方法可以让它们随机生成。所以不要看起来像这个()()()().. 他们看起来更像是这个()。 ()。 ()()。非常感谢任何花时间回应的人。

最终输出

           ^
          /|\
   . (  ) /|\    .   (  )     ( )

程序:

import java.util.ArrayList;

public class ForestRandomizer {

    public static String forestGen()
    {
            String bush = "( )";
            ArrayList<String> bushes = new ArrayList<String>();

            for(int n = 0; Walking.bushesInArea > n; n++)
            {
                bushes.add(bush);
            }

            String rock = ".";
            ArrayList<String> rocks = new ArrayList<String>();

            for(int n = 0; Walking.rocksInArea > n; n++)
            {
                rocks.add(rock);
            }


            System.out.println();
            System.out.println();
            System.out.println();
            for(int i = 0; i < bushes.size(); i++)
            {
                System.out.print(bushes.get(i) + "  ");
            }
            for(int i = 0; i < rocks.size(); i++)
            {
                System.out.print(rocks.get(i) + "  ");
            }
            System.out.println();

        return null;
    }
}

2 个答案:

答案 0 :(得分:1)

这个程序有点粗糙......但它完成了工作。我已经使用注释进行了逐行解释。该程序在底行的随机位置打印岩石和灌木,还打印一棵高大的树,占用4行。这是程序:

String[] choices = {"() ",".  "}; //choices holds 2 members, () and .
Random rand = new Random();       //creates random method
int place = rand.nextInt(10);     // this is for the tree
for (int j = 0; j<4; j++){
    for (int i = 0; i < 10; i++) {                //executes the following loop 10 times
        if (j==3){                                //makes rocks and bushes print only on bottom line
            int NumberOfAnswers = choices.length;     //holds value of number of members in choices
            int pick = rand.nextInt(NumberOfAnswers); // picks random int 0 or 1
            String finalChoice = choices[pick];       //"finalChoice" is either member 0 or 1 of choices
            System.out.print(finalChoice);            // prints "finalChoice"
        } else {
            System.out.print("   ");
        }
        if (i == place && j == 0) {                 //if it's the designated place and the top row...
            System.out.print(" ^  ");               //...print the top of the tree
        } else if (i==place && (j == 1||j == 2)) {  //if it's the designated place in the 2nd or 3rd rows...
            System.out.print("/|\\ ");              //...print the body of the tree
        } else if (i == place && j == 3){           //if its the designated place and the last row
            System.out.print(" |  ");               //...print the base of the tree
        }
    }   
    System.out.println();                         //start a new line
}

输出的2个例子:

------------- ------------- 1

    ^                             
   /|\                            
   /|\                            
.   |  () .  () .  .  () .  .  .  

------------- ------------- 2

                         ^        
                        /|\       
                        /|\       
() .  () () .  .  .  ()  |  () () 

答案 1 :(得分:1)

一种简单的方法是只添加一个ArrayList,然后在该列表中使用Collections.shuffle()

例如:

public static String forestGen(int numberBushes, int numberRocks) {
    String bush = "( )";
    String rock = ".";
    ArrayList<String> elements = new ArrayList<>(numberBushes + numberRocks);
    for (int i = 0; i < numberBushes; i++) {
        elements.add(bush);
    }
    for (int i = 0; i < numberRocks; i++) {
        elements.add(rock);
    }

    Collections.shuffle(elements);

    StringBuilder sb = new StringBuilder();
    for(String element : elements) {
        sb.append(element).append(" ");
    }
    return sb.toString();
}

创建

等输出
. ( ) . ( ) . ( ) ( ) . ( )
. ( ) . ( ) ( ) ( ) ( ) . . 
( ) ( ) . ( ) ( ) ( ) . . . 
相关问题