0

我正在学习 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);



}
}
4

3 回答 3

2

您声明长度为 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.

于 2013-10-22T02:42:29.593 回答
0

当您仅排列 20 个长度时

那么这个

for (int i = 0; i < 21; i++) {

会导致溢出

它应该是< 20

另外,此代码毫无意义,因为您的密钥只是 2 的数组

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]);
于 2013-10-22T02:40:43.833 回答
0
String[] key = {"",""};
...
for (int i = 0; i < 21; i++) {
    ...
    key[i] = ....

你看到问题了吗?

于 2013-10-22T02:43:07.530 回答