简体   繁体   中英

Why are my if statements not working in this for loop?

Question:

I'm designing a small Android app that plays sounds (notes) when you press an imageButton that looks like a specific instrument, and you can play up to 10 notes across 6 imageButtons. It stores the note number in an array and sets the element to a specific number. This part seems to work fine

The app has a feature that lets you play back what you recently played. For some reason this only plays the first note 10 times - it doesn't seem to understand the new array values so it always gets hung up on the first if statement in the for loop. I need it to read the new array values so it can play back what the user played on the given instrument.

Code:

public void drum1button(View view) { //This code snippet works as expected
    if (studio.drumCount < 10){
        new Thread(drum.drum1).start();
        drum.drums[studio.drumCount] = 0;
        Toast.makeText(getApplicationContext(),studio.drumCount + " drum.drums[studio.drumCount] = " + drum.drums[studio.drumCount],2).show();

        studio.drumCount++;
    }
    else {
    Toast.makeText(getApplicationContext(),"Record limit reached. Head to the studio!",5).show();
    }
}


public void drum2button(View view) {
if (studio.drumCount < 10){
    new Thread(drum.drum2).start();
    drum.drums[studio.drumCount] = 1;
    Toast.makeText(getApplicationContext(),studio.drumCount + " drum.drums[studio.drumCount] = " + drum.drums[studio.drumCount],2).show();

    studio.drumCount++;

}
else {
Toast.makeText(getApplicationContext(),"Record limit reached. Head to the studio!",5).show();
}
}


public void drum3button(View view) {
    if (studio.drumCount <10 ) {
    new Thread(drum.drum3).start();
    drum.drums[studio.drumCount] = 2;
    Toast.makeText(getApplicationContext(),studio.drumCount + " drum.drums[studio.drumCount] = " + drum.drums[studio.drumCount],2).show();

    studio.drumCount++;
    }
    else {
    Toast.makeText(getApplicationContext(),"Record limit reached. Head to the studio!",5).show();
    }

}

public void drum4button(View view) {
    if (studio.drumCount < 10) {
    new Thread(drum.drum4).start();
    drum.drums[studio.drumCount] = 3;
    Toast.makeText(getApplicationContext(),studio.drumCount + " drum.drums[studio.drumCount] = " + drum.drums[studio.drumCount],2).show();

    studio.drumCount++;
    }
    else {
    Toast.makeText(getApplicationContext(),"Record limit reached. Head to the studio!",5).show();
    }
}

public void drum5button(View view) {
    if (studio.drumCount < 10) {
    new Thread(drum.drum5).start();
    drum.drums[studio.drumCount] = 4;
    Toast.makeText(getApplicationContext(),studio.drumCount + " drum.drums[studio.drumCount] = " + drum.drums[studio.drumCount],2).show();

    studio.drumCount++;
    }

    else {
    Toast.makeText(getApplicationContext(),"Record limit reached. Head to the studio!",5).show();
    }
}

public void drum6button(View view) {
    if (studio.drumCount < 10){
    new Thread(drum.drum6).start();
    drum.drums[studio.drumCount] = 5;
    Toast.makeText(getApplicationContext(),studio.drumCount + " drum.drums[studio.drumCount] = " + drum.drums[studio.drumCount],2).show();

    studio.drumCount++;
    }
    else {
        Toast.makeText(getApplicationContext(),"Record limit reached. Head to the studio!",5).show();

    }
}

Runnable drumPlayback = new Runnable() {

    public void run() {
    for (int i = 0; i < 10; i++) { //(was i = 10, typo in formatting when //pasting. Still does not work with i = 0
        //handler.postDelayed(drumPlayback, 5000);

        try {Thread.sleep(1000);} catch(Exception e) {}
         if (drum.drums[i] == 0) { 
          new Thread(drum.drum1).start(); //this is the only one that plays

          }
          if (drum.drums[i] == 1) { 
          new Thread(drum.drum2).start(); 
          } 
         if (drum.drums[i] == 2) { 
          new Thread(drum.drum3).start(); 
          } 
           if (drum.drums[i] == 3) { 
          new Thread(drum.drum4).start(); 
          } 
           if (drum.drums[i] == 4) { 
          new Thread(drum.drum5).start(); 
          } 
           if (drum.drums[i] == 5) {
          new Thread(drum.drum6).start(); 
                    }
                }
            }
    };

循环条件是i < 10但是您使用10初始化i。因此循环将永远不会开始。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM