jeudi 4 juin 2015

Changing an already made randomly generated number to an int: Java

So, I am an absolute neophyte, please forgive me:

First an explanation: The programming assignment that I am doing has me generating skydiving runs, made out of blocks. To do that, I needed (first off) to get a random number between 1 and 38. I used a random number generator I found online and took out the parts I don't need. As is, the code spits back a series of numbers:

import java.util.Random;

/** Generate random integers in a certain range. */
public final class DiveGen {

  public static final void main(String... aArgs){
    log("Generating Runs");
    for (int runs=1; runs<=10; runs++) {
    int START = 1;
    int END = 37;
    Random block = new Random();
    for (int moves = 1; moves <= 4; ++moves){
           showRandomInteger(START, END, block);
            }
      System.out.printf("\n");
    }
  }

  private static void showRandomInteger(int aStart, int aEnd, Random aRandom){
    if (aStart > aEnd) {
      throw new IllegalArgumentException("Start cannot exceed End.");
    }
    //get the range, casting to long to avoid overflow problems
    long range = (long)aEnd - (long)aStart + 1;
    // compute a fraction of the range, 0 <= frac < range
    long fraction = (long)(range * aRandom.nextDouble());
    int randomNumber =  (int)(fraction + aStart);    
    System.out.printf(" " + randomNumber);
    }
    {
    System.out.printf("\n");
    }

  private static void log(String aMessage){
    System.out.println(aMessage);
  }
} 

I have (after much tinkering) managed to get a random number generator to do what I want it to do, it generates 10 groups of 4 integers between 1 and 38.

My next step is: using a switch command, I want for it to assign a name to each value using a switch command:

String blockString = new String
      {
        switch (randomblock) {
                case 1:blockString="Unipod, ";
                case 2:blockString="Stairstep Diamond, ";
                case 3:blockString="Murphy Flake, ";
                case 4:blockString="Yuan, ";
                case 5:blockString="Meeker, ";
                case 6:blockString="Open Accordian, ";
                case 7:blockString="Cataccord, ";
                case 8:blockString="Bow, ";
                case 9:blockString="Donut, ";
                case 10:blockString="Hook, ";
                case 11:blockString="Adder, ";
                case 12:blockString="Star, ";
                case 13:blockString="Crank, ";
                case 14:blockString="Satellite, ";
                case 15:blockString="Sidebody, ";
                case 16:blockString="Phalanx, ";
                case 17:blockString="Snowflake/Inter/Snowflake, ";
                case 18:blockString="Sidebody Donut/Inter/Side Flake Donut, ";
                case 19:blockString="Side Flake Opal/Inter/Turf, ";
                case 20:blockString="Monopod/Inter/Monopod, ";
                case 21:blockString="Opal/Inter/Opal, ";
                case 22:blockString="Stardian/Inter/Stardian, ";
                case 23:blockString="Sidebuddies/Inter/Sidebuddies, ";
                case 24:blockString="Canadian Tee/Inter/Canadian Tee, ";
                case 25:blockString="Cat + Accordian/Inter/Cat + Accordian, ";
                case 26:blockString="Diamond/Inter/Bunyip, ";
                case 27:blockString="Photon/Inter/Photon, ";
                case 28:blockString="Bundy/Inter/Bundy, ";
                case 29:blockString="Offset/Inter/Offset, ";
                case 30:blockString="Bipole/Inter/Bipole, ";
                case 31:blockString="Caterpillar/Inter/Caterpillar, ";
                case 32:blockString="Compressed Accordian/Inter/Box, ";
                case 33:blockString="Danish Tee/Inter/Murphy, ";
                case 34:blockString="Zircon/Inter/Zircon, ";
                case 35:blockString="Ritz/Inter/Icepick, ";
                case 36:blockString="Piver/Inter/Viper, ";
                case 37:blockString="Zig Zag/Inter/Marquis, ";
                case 38:blockString="Tee/Inter/Chinese Tee, ";
                }
      }
                System.out.printf(blockString);

What I want to know is: how would I stick these two together? Instead of printing out the generated integers, I would like for it to print out the names of the maneuvers. So far, the best I can get is an error message saying "Missing { or [", at one point (I can't even remember how I got it) it told me that I couldn't use a random number as an integer.

The next part of my code is also going to require that I different numbers be worth different point values (1-16 are worth 1 point, 17-38 are worth two points). I'll have to consider whether something is a 'legal' run or not, but that is a different question (albeit one I have to keep in mind).

I apologize for rambling: what can I do?




Aucun commentaire:

Enregistrer un commentaire