Java 在处理过程中,如何创建一个方法来搜索一个数组的最大值?
我试图创建一个方法,该方法搜索volume数组中的最大值,并返回note数组相应位置的值 另外,我如何修改它,使其只播放最响亮的音符Java 在处理过程中,如何创建一个方法来搜索一个数组的最大值?,java,arrays,processing,midi,Java,Arrays,Processing,Midi,我试图创建一个方法,该方法搜索volume数组中的最大值,并返回note数组相应位置的值 另外,我如何修改它,使其只播放最响亮的音符 import arb.soundcipher.*; SoundCipher midi; String[] note = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"}; float[] volume = {80, 100, 75, 43,
import arb.soundcipher.*;
SoundCipher midi;
String[] note = {"C", "C#", "D", "D#", "E", "F", "F#",
"G", "G#", "A", "A#", "B"};
float[] volume = {80, 100, 75, 43, 40, 81, 100,
60, 90, 30, 75, 52};
float[] duration = {1.3, 2, 0.5, 3, 0.9, 1, 0.25,
0.6, 1.5, 3, 1.25, 2};
void setup() {
size(200,200);
midi = new SoundCipher(this);
int i = (int) random(note.length);
midi.playNote(MIDIValue(note[i]),volume[i],duration[i]);
}
float MIDIValue(String aNote) {
float noteMIDIValue = 0.0;
int i;
for (i =0; i < note.length && !note[i].equals(aNote) ; i++);
if(i < note.length) {
noteMIDIValue = i+60;
}
return noteMIDIValue;
}
导入arb.soundcipher.*;
声音密码midi;
String[]note={“C”、“C”、“D”、“D”、“E”、“F”、“F”,
“G”、“G”、“A”、“A”、“B”};
浮动[]体积={80,100,75,43,40,81,100,
60, 90, 30, 75, 52};
浮动[]持续时间={1.3,2,0.5,3,0.9,1,0.25,
0.6, 1.5, 3, 1.25, 2};
无效设置(){
规模(200200);
midi=新的声音密码(此);
int i=(int)随机(注:长度);
midi.playNote(MIDIValue(note[i])、volume[i]、duration[i]);
}
浮点值(字符串aNote){
浮点值=0.0;
int i;
对于(i=0;i
遍历卷
数组并跟踪最大卷和id
(索引)。最后,返回与最大元素id(节点[id]
)对应的注释
方法实现
String findLargestVolumeNote() {
int maxId = 0;
float maxVol = volume[0];
// searching array for loudest volume
for(int i = 0; i < volume.length; i++) {
if(volume[i] > maxVol) {
maxId = i;
maxVol = volume[i];
}
}
return note[maxId]; // returning corrsponding note
}
String findLargestVolumeNote(){
int maxId=0;
float maxVol=体积[0];
//搜索最大音量的阵列
对于(int i=0;imaxVol){
maxId=i;
maxVol=体积[i];
}
}
返回注释[maxId];//返回对应注释
}