mercredi 6 mai 2020

If we are casting when using (int)(Math.random()), wouldn't our variable be assigned zero even if we multiplied by another value?

I'm running through some mini-programming projects in a textbook and I was wondering, if we're casting to an int type in the following program, wouldn't oneLength, twoLength, threeLength all be assigned a value of 0? I know that since we are casting to an int, the values will be from 0 to 1, and this means that 0 will be inclusive, and 1 exclusive. If we have 0 * (12), 12 being the length of wordListOne as an example, wouldn't the final value be 0?

Here is the program:

public class ScribblePad {

public static void main(String[] args) {

    String[] wordListOne = {"24/7", "multi-Tier", "30,000 foot", "B-to-B", "win-win", "front-end", "web-based", "pervasive", "smart", "six-sigma", "critical-patch", "dynamic"};

    String[] wordListTwo = {"empowered", "sticky", "value-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"};


    // oneLength : number of items in array

    int oneLength = wordListOne.length;
    int twoLength = wordListTwo.length;
    int threeLength = wordListThree.length;

    // rand1 : casting to int value

    int rand1 = (int) (Math.random() * oneLength);
    int rand2 = (int) (Math.random() * twoLength);
    int rand3 = (int) (Math.random() * threeLength);


    // phrase : 

    String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];

    System.out.println("What we need is a " + phrase); }}

Really appreciate the help I get, and thank you so much.




Aucun commentaire:

Enregistrer un commentaire