将已经生成的随机生成的数字更改为int:Java

时间:2015-06-04 19:00:32

标签: java random switch-statement

所以,我是绝对的新手,请原谅我:

首先解释一下: 我正在做的编程任务让我生成了由块组成的跳伞运行。为此,我需要(首先关闭)获得1到38之间的随机数。我使用了一个在线发现的随机数生成器并取出了我不需要的部分。按原样,代码会回吐一系列数字:

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);
  }
} 

我(经过多次修补)设法得到一个随机数生成器来做我想做的事情,它生成10组4个整数,介于1到38之间。

我的下一步是:使用switch命令,我希望它使用switch命令为每个值分配一个名称:

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);

我想知道的是:我如何将这两者结合在一起?我不想打印出生成的整数,而是打印出演习的名称。到目前为止,我能得到的最好的是一条错误消息,说“错过{或[”,在某一点上(我甚至不记得我是怎么得到的)它告诉我我不能使用随机数作为整数

我的代码的 next 部分也将要求我不同的数字值得不同的点值(1-16值1点,17-38值2点)。我将不得不考虑某些事情是否属于“合法”,但这是一个不同的问题(尽管我必须牢记这一点)。

我为漫无边际道歉:我该怎么办?

1 个答案:

答案 0 :(得分:0)

只需将blockString创建为String Array并将其与数字一起用作索引:

static String blockString[] = { " ", "Unipod", "Stairstep Diamond",
        "Murphy Flake", "Yuan", "Meeker", "Open Accordian",.....};

//use as required
System.out.printf(" " + randomNumber + " " + blockString[randomNumber]);
Or
System.out.printf(" " + blockString[randomNumber]);