[英]Why are my if statements not working in this for loop?
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. 我正在设计一个小型的Android应用程序,当您按下看起来像特定乐器的imageButton时会播放声音(音符),并且可以在6个imageButtons中最多播放10个音符。 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.
由于某种原因,它只会播放前一个音符10次-似乎不了解新的数组值,因此它总是挂在for循环中的第一个if语句上。 I need it to read the new array values so it can play back what the user played on the given instrument.
我需要它来读取新的数组值,以便可以播放用户在给定乐器上演奏的内容。
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。因此循环将永远不会开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.