java - java中随机数组与随机数组相加,使其等于另一个数组

标签 java arrays random

我正在学习java,据我所知,我想要做的事情是一种罕见的情况。

我正在尝试使用 API (kindof) 来随机生成音符。我希望它生成 20 次,所以我将它放在 for 循环中。我意识到我可以为此使用一个列表,我只是不知道如何实现它。我的问题是,当我尝试编译此代码时,第一部分会运行。它让我可以制作种子。然而之后它给了我

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at co.kbcomp.music.Main.main(Main.java:64)

我可以做什么来防止这种情况发生?我知道我所做的事情是错误的。这些就不用我说了。我想知道我哪里出错了。

package co.kbcomp.music;

import java.util.*;
import org.jfugue.*;
import java.util.Scanner; 

public class Main {

public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter a number for the seed of the song");
    long seed = in.nextLong();

    Calendar cal = Calendar.getInstance();
    Random rand;
    //rand = new Random(cal.getTime());
    rand = new Random(seed);

    int NoteNumber = 0;
    int NoteLength = 0; 
    int OctiveNumber = 0;
    int ChordNumber = 0;
    int InversionNumber = 0;
    //int Duration = rand.nextInt(100 - 5) + 5;

    //This keeps track of the iteration of the for loop.
    String[] NN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
    String[] NL = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
    String[] IN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
    String[] CN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
    String[] ON = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};

    //This is what is being played
    String[] note = { "A", "B", "C", "D", "E", "F", "G" };
    String[] noteLength = {"", "w", "", "h", "", "q","", "i"};
    String[] octive = { "","1","",  "2", "", "3", "", "4", "", "5", "", "6", "", "7", "", "8", "", "9"};
    String[] chord = { "", "maj", "", "min"};
    String[] inversion = {"", "^", "", "^^", "", "^^^", "", "^^^^", "", "^^^^^"};
    String[] key = {"",""};
    String keys= " ";

    String randstr = " ";

    //this is the loop that defines the music legnth
    for (int i = 0; i < 21; i++) {

        NoteNumber = rand.nextInt(7);
        NoteLength = rand.nextInt(8);
        OctiveNumber = rand.nextInt(18);
        ChordNumber = rand.nextInt(4);
        InversionNumber = rand.nextInt(10);



        NN[i] = note[NoteNumber];           // This randomly generates the note to be played.
        NL[i] = noteLength[NoteLength];     // This randomly generates the length of the note.
        ON[i] = octive[OctiveNumber];       // This defines the octive to be played in.
        CN[i] = chord[ChordNumber];         // This is defines the major or the minor
        IN[i] = inversion[InversionNumber]; // IN[i] = inversion[InversionNumber];


        key[i] = NN[i] + NL[i] + ON[i] + CN[i] + IN[i];
        //randstr = c[0] + " " + c[1] + " " + c[2] + " " + c[3] + " " + c[4] + " " + c[5] + " " + c[6] + " " + c[7] + " " + c[8] + " " + c[9] + " " + c[10] + " " + c[11] + " " + c[12] + " " + c[13] + " " + c[14] + " " + c[15] + " " + c[16];
        keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] + " " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] +  " " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " + key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] + " " + key[19] + " " + key[20]);
    }


    System.out.println(key);

    Player player = new Player();
    Pattern pattern = new Pattern(key[0]);
    player.play(pattern);



}
}

最佳答案

您声明长度为 2 的键数组:

String[] key = {"",""};

但是稍后在 for 循环中,您尝试访问超出数组长度的元素:

keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] + 
       " " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] + 
       " " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " +
       key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] + 
       " " + key[19]  + " " + key[20]);

由于数组的长度只有 2,因此当您尝试访问第三个元素(位于数组索引 2 处)时,您会收到 ArrayIndexOutOfBoundsException

关于java - java中随机数组与随机数组相加,使其等于另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19508088/

相关文章:

java - 如何使用java进程一条一条执行windows命令

java - 在 jQAssistant 查询中排除内部类

java - 生成带有 minifyEnabled 设置为 true 的签名 APK 后,firebase UI 输出空白

存储在字符串中的字符 - 如何在随机化时将它们存储在另一个字符串变量中

java - 从java中的类调用对象

python - 使用其索引存在于数据帧的一列中的列表在 PySpark 数据帧中创建一列

java - 如何将 int[] 转换为 byte[]

来自索引数组的python numpy数组

php - PHP7中的随机字符串

javascript - 随机生成的本地存储 key