public class PhraseOMatic {
public static void main (String[] args) { // make three sets of words to choose from
String[] wordListOne = {"24/7","multi-Tier","30,000 foot","B-to-B","win-win","front-end", "web-based","pervasive", "smart", "six-sigma","critical-path", "dynamic"}; String[] wordListTwo = {"empowered", "sticky", "valued-added", "oriented", "centric", "distributed", "clustered", "branded","outside-the-box", "positioned", "networked", "focused", "leveraged", "aligned", "targeted", "shared", "cooperative", "accelerated"}; String[] wordListThree = {"process", "tipping point", "solution", "architecture", "core competency", "strategy", "mindshare", "portal", "space", "vision", "paradigm", "mission"}; // find out how many words are in each list
int oneLength = wordListOne.length;
int twoLength = wordListTwo.length;
int threeLength = wordListThree.length; // generate three random numbers, to pull random words from each list
int rand1 = (int) (Math.random() * oneLength);
int rand2 = (int) (Math.random() * twoLength);
int rand3 = (int) (Math.random() * threeLength); // now build a phrase
String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3]; // now print it
System.out.println("What we need is a " + phrase);
}
} rand1, rand2, rand3不会越界么?如果不会那是为什么呢,如果会又是为什么
/*不会越界,原因如下:
Math.random()会产生一个[0,1),也就0到1的随机浮点数,注意包括0,但不包括1
(int)Math.random()*wordListOne.length,会产生一个0到wordlistOne.length之间的整数,不包括wordlistOne.length,因为Math.random()不能产生1
所以不会越界/*