繁体   English   中英

如何从字符串数组中打印随机单词? 爪哇

[英]How do I print a random word from a string array? Java

我必须使代码中的三个数组(如下所示)才能随机打印。 因此,对于“今天:”和“明天”,打印随机的好坏评论。 对于您的幸运和不太幸运的日子,请为幸运的日子打印一个随机日期和一个随机的好评论,为不太幸运的日子打印一个随机的坏评论。 我不知道如何从字符串数组中随机选择。 我明天就可以得到这个,几个月前我才开始编写代码,所以我不确定如何去做。

今天:您将遇到一个老朋友! 明天:相信您的直觉。 宇宙在指引你的生活。 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"};

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

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

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

解决方案将是从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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM