如何从字符串数组中打印随机字? Java的

时间:2015-11-12 03:25:39

标签: java arrays

我必须在我的代码中创建三个数组(如下所示)以随机打印。所以对于"今天:"和"明天"打印一个随机的好评或坏评论。为了您的幸运而不是那么幸运的日子,请为幸运日打印随机日期和随机好评,并为不那么幸运的日子随机发表评论。我不知道如何从字符串数组中随机选取。我明天就有这个,几个月前我刚刚开始编码,所以我不知道我会怎么做。

今天:你会遇到一位老朋友! 明天:相信你的直觉。宇宙正在指导你的生活。 您2016年的幸运日是11月4日:您将赢得累积奖金。 您2016年不太幸运的一天是1月24日:停电日

这是我的代码:

String Month[]={"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
String goodcomment[]={"None of the secrets of success will work unless you do","Today is a lucky day for those who remain cheerful and optimistic","You were born with the skill to communicate with people easily","The first step to better times is to imagine them","Trust your intuition. The Universe is your guiding light","It doesnt matter. Who is without a flaw?","Your happiness is interwined with your outlook on life","The secret of getting ahead is getting started", "Failure will never overtake you if my determination to succeed is strong enough.","What you do today can improve all your tomorrows.","In order to succeed, you must first believe that you can.","Always do your best. What you plant now, you will harvest later."};
String badcomment[]={"Power failure today","You'll miss the bus","Too many things to do and not enough time","University entranced denied","You'll lose your phone","You'll miss your date","You'll forget someone close to you's birthday", "Today is a disastrous day. If you can’t beat ’em, join ’em","You will meet someone next week that will bring negativity into your life","Don't step on a crack","You will wake up sad tomorrow","Your car will break down"};

2 个答案:

答案 0 :(得分:1)

您可以使用Random类。方法 nextInt(int bount)将返回从0到bound-1的随机整数。

因此,要从数组中随机选择一个字符串,您可以执行以下操作:

Random random = new Random();
int randomIdx = random.nextInt(goodComment.length);
System.out.println(goodComment[randomIdx]);

答案 1 :(得分:0)

解决方案是从0或1中选择一个随机数来从好的或坏的评论中随机选择(即0 - 好评和1-评论)。然后通过获得从0到(所选数组的长度-1)的随机数从所选数组中随机选择注释,以指示打印注释的索引。您可以使用tixopi建议的方法生成随机数。

private String goodcomment[] = { "None of the secrets of success will work unless you do",
        "Today is a lucky day for those who remain cheerful and optimistic"};
private String badcomment[] = { "Power failure today", "You'll miss the bus", "Too many things to do and not enough time",
        "University entranced denied", "You'll lose your phone", "You'll miss your date"};

private Random indexGenerator = new Random();

public void printCommentWithPrefix(String prefix) {
    System.out.println(prefix + " : " + selectRandomGoodOrBadComment());
}

private String selectRandomGoodOrBadComment() {
    String randomGoodOrBadComment;
    if (selectEitherGoodOrBad() == 0) {
        randomGoodOrBadComment = selectAComment(goodcomment);
    } else {
        randomGoodOrBadComment = selectAComment(badcomment);
    }
    return randomGoodOrBadComment;
}

private int selectEitherGoodOrBad() {
    int goodOrBadIndicator = indexGenerator.nextInt(2);
    return goodOrBadIndicator;
}

private String selectAComment(String[] comment) {
    int indexOfSelectedComment = indexGenerator.nextInt(comment.length);
    String selectedComment = comment[indexOfSelectedComment];
    return selectedComment;
}
相关问题