gpt4 book ai didi

java - 为什么我的 if 语句在这个 for 循环中不起作用?

转载 作者:行者123 更新时间:2023-12-01 18:48:43 24 4
gpt4 key购买 nike

问题:

我正在设计一个小型 Android 应用程序,当您按下看起来像特定乐器的 imageButton 时,它会播放声音(音符),并且您可以在 6 个 imageButton 上演奏最多 10 个音符。它将音符编号存储在数组中并将元素设置为特定编号。这部分似乎工作正常

该应用程序有一项功能,可让您播放最近播放的内容。由于某种原因,它只播放第一个音符 10 次 - 它似乎不理解新的数组值,因此它总是卡在 for 循环中的第一个 if 语句上。我需要它来读取新的数组值,以便它可以播放用户在给定乐器上演奏的内容。

代码:

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 。所以循环永远不会开始。

关于java - 为什么我的 if 语句在这个 for 循环中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16597449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com