我有一堆 int 值,比如 10、5、4、2、1。我有一堆Double
值,List<Double>
比如 0.65 等等。
如何通过此列表检查每个 Double 最接近的 int 值?
我的代码是用于 android 音乐应用程序的。这就是我最终解决它的方法。
@Override
public void onClick(View v) {
String[] noteNames = new String[9];
double[] notes = new double[9];
double crotchet = 60.0 / Long.parseLong(setBPM.getText().toString());
double quaver = crotchet / 2.0;
double tripletQuaver = crotchet / 3.0;
double dottedCrotchet = crotchet + quaver;
double semiquaver = quaver / 2.0;
double dottedQuaver = quaver + semiquaver;
double minum = crotchet * 2.0;
double dottedMinum = minum + crotchet;
double semibreve = minum * 2.0;
notes[0] = semiquaver;
notes[1] = tripletQuaver;
notes[2] = quaver;
notes[3] = dottedQuaver;
notes[4] = crotchet;
notes[5] = dottedCrotchet;
notes[6] = minum;
notes[7] = dottedMinum;
notes[8] = semibreve;
noteNames[0] = "semiquaver";
noteNames[1] = "tripletQuaver";
noteNames[2] = "quaver";
noteNames[3] = "dottedQuaver";
noteNames[4] = "crotchet";
noteNames[5] = "dottedCrotchet";
noteNames[6] = "minum";
noteNames[7] = "dottedMinum";
noteNames[8] = "semibreve";
for(double l : notes) {
System.out.println(l);
}
double[] tempCurrentDiff = new double[9];
double[] currentDiff = new double[9];
for (Double d : beats) {
for (int i = 0; i < 9; i++) {
currentDiff[i] = Math.abs(notes[i] - d);
tempCurrentDiff[i] = Math.abs(notes[i] - d);
}
Arrays.sort(tempCurrentDiff);
for (int i = 0; i < 9; i++){
if (currentDiff[i] == tempCurrentDiff[0]) {
System.out.println(noteNames[i]);
notesView.append(noteNames[i] + " ");
break;
}
}
}
}