Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 出界,_Java_String_Indexing - Fatal编程技术网

Java 出界,

Java 出界,,java,string,indexing,Java,String,Indexing,这个程序是用键盘键来播放音符的。对于我按下的每个键,我都会得到一个超出范围的不同字符串索引,从1的49到m的109。但我总是收到这个错误信息。我是Java新手,任何帮助都将不胜感激,因为我查看了很多论坛,还没有找到解决此类问题的答案 在此行引发异常: nextnote = keyboard.charAt(key); 这是我的代码: public class GuitarHero { public static void main(String[] args) { //

这个程序是用键盘键来播放音符的。对于我按下的每个键,我都会得到一个超出范围的不同字符串索引,从1的49到m的109。但我总是收到这个错误信息。我是Java新手,任何帮助都将不胜感激,因为我查看了很多论坛,还没有找到解决此类问题的答案

在此行引发异常:

nextnote = keyboard.charAt(key);
这是我的代码:

public class GuitarHero {
    public static void main(String[] args) {
        //make array for strings
        double[] notes = new double[37];
        GuitarString[] strings = new GuitarString[37];
        int nextnote;
        int firstnote=0;
        double NOTE = 440.0;
        String keyboard ="1234567890qwertyuiopasdfghjklzxcvbnm";
        //for loop to set notes
        for(int i=0;i<37;i++){
            double concert = 440.0* Math.pow(2, (i-24)/12.0);
            notes[i] = concert;
            for(int j=0;j<37;j++){
                strings[j] = new GuitarString(concert);
            }
        }
        while (true) {
            // check if the user has typed a key; if so, process it
            if (StdDraw.hasNextKeyTyped()) {
                char key = StdDraw.nextKeyTyped();
                //charAt gets index of character in string 
                nextnote = keyboard.charAt(key);
                //make sure value is within string
                if(nextnote>=0 && nextnote<37){
                    // pluck string and compute the superposition of samples
                strings[nextnote].pluck();
                    double sample = strings[firstnote].sample() 
                          +strings[nextnote].sample();
                    StdAudio.play(sample);
                    // advance the simulation of each guitar string by one step   
                    strings[nextnote].tic();
                    firstnote=nextnote;
                }
        }
        }
    }
}
公共类吉他英雄{
公共静态void main(字符串[]args){
//为字符串生成数组
double[]注释=新的double[37];
GuitarString[]字符串=新的GuitarString[37];
int nextnote;
int firstnote=0;
双音符=440.0;
字符串键盘=“1234567890qwertyuiopasdfghjklzxcvnm”;
//为循环设置注释
for(int i=0;i您要调用,它将为您提供字符的索引。返回给定索引处的字符。

您要调用,它将为您提供字符的索引。返回给定索引处的字符。

您需要该方法

返回此字符串中第一次出现的指定字符的索引

而不是

返回指定索引处的字符值。索引的范围从0到length()-1。序列的第一个字符值位于索引0处,下一个字符值位于索引1处,依此类推,用于数组索引

你需要这个方法

返回此字符串中第一次出现的指定字符的索引

而不是

返回指定索引处的字符值。索引的范围从0到length()-1。序列的第一个字符值位于索引0处,下一个字符值位于索引1处,依此类推,用于数组索引

问题在于:
StdDraw.nextKeyTyped();
文档说明:

用户键入的下一个键是什么?此方法返回 与键入的键(如“a”或“a”)对应的Unicode字符。 它无法识别动作键(如F1和箭头键)或修改器 按键(如控制键)

是一个字符,不是此行的索引。请改为执行以下操作:

int charIndexInKeyboard = keyboard.indexOf(key);
if(charIndexInKeyboard == -1) // char not recognized
nextnote = keyboard.charAt(charIndexInKeyboard );
nextnote
现在应该包含所需的字符

编辑:下面是while循环现在的样子

while (true) {
    // check if the user has typed a key; if so, process it
    if (StdDraw.hasNextKeyTyped()) {
        char key = StdDraw.nextKeyTyped();
        int charIndexInKeyboard = keyboard.indexOf(key);
        if(charIndexInKeyboard == -1){
            // Not recognized, just continue to next
            continue;
        }
        nextnote = keyboard.charAt(charIndexInKeyboard);
        // pluck string and compute the superposition of samples
        strings[nextnote].pluck();
        double sample = strings[firstnote].sample()
        +strings[nextnote].sample();
        StdAudio.play(sample);
        // advance the simulation of each guitar string by one step
        strings[nextnote].tic();
        firstnote=nextnote;
    }
}
问题在于:
StdDraw.nextKeyTyped();
文档说明:

用户键入的下一个键是什么?此方法返回 与键入的键(如“a”或“a”)对应的Unicode字符。 它无法识别动作键(如F1和箭头键)或修改器 按键(如控制键)

是一个字符,不是此行的索引。请改为执行以下操作:

int charIndexInKeyboard = keyboard.indexOf(key);
if(charIndexInKeyboard == -1) // char not recognized
nextnote = keyboard.charAt(charIndexInKeyboard );
nextnote
现在应该包含所需的字符

编辑:下面是while循环现在的样子

while (true) {
    // check if the user has typed a key; if so, process it
    if (StdDraw.hasNextKeyTyped()) {
        char key = StdDraw.nextKeyTyped();
        int charIndexInKeyboard = keyboard.indexOf(key);
        if(charIndexInKeyboard == -1){
            // Not recognized, just continue to next
            continue;
        }
        nextnote = keyboard.charAt(charIndexInKeyboard);
        // pluck string and compute the superposition of samples
        strings[nextnote].pluck();
        double sample = strings[firstnote].sample()
        +strings[nextnote].sample();
        StdAudio.play(sample);
        // advance the simulation of each guitar string by one step
        strings[nextnote].tic();
        firstnote=nextnote;
    }
}

调用时键的值是多少?在字符串
键盘
中有37个字符,这些字符占据索引0-36。因此,如果键>大于36或<0,则您将获得
索引自动边界异常
调用时键的值是多少?字符串
键盘
中有37个字符因此,如果key>36或<0,那么你会得到
IndexOutOfBoundsException
@bitva,很高兴它能起作用。为了将来的参考和帮助你的同事找到正确的答案,请接受这篇文章作为你的答案:)@bitva很高兴它成功了。为了将来的参考和帮助您的同事找到正确的答案,请接受此帖子作为答案,如果它对您有效:)