在Java中,Double是最接近的int

时间:2013-11-07 05:55:47

标签: java

我有一堆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;
                        }
                    }
                }
          }

5 个答案:

答案 0 :(得分:3)

使用Math.round(double)功能对您列表中的每个元素进行舍入。 Math.round(double)最接近的 long返回到参数,并将(0.5)四舍五入。但是,如果参数为NaN,则结果为0

所以

  1. 使用int[] a对给定的整数数组Arrays.sort(a)进行排序,如果尚未排序
  2. 查看每个双x
  3. 的列表
  4. 查找整数key = Math.round(x)
  5. 使用Arrays.binarySearch(a, key);其中a是包含整数数组元素的int[]。如果数组negative index中不存在该元素,则此函数可能返回anegative index: (-(insertion point) - 1).表示我们正在搜索的数组的插入点。
  6. 示例代码:

    double doub[] = {0.5, 2.6, 3.7, 1.7, 4.6};
      long a[] = {1, 4, 3}; //
    
      Arrays.sort(a);
      for(double x : doub)
      {
          long key = Math.round(x);
          int index = Arrays.binarySearch(a, key);
          if(index < 0) // element wasn't found
               index = - index -1;
          if(index > a.length -1) // key is greater than the maximum element 
             index = a.length - 1;
    
          System.out.println("double: "+x+" rounded key: "+key+" value: "+a[index]);
    
      }
    

答案 1 :(得分:2)

首先,使用Math.round(),因为其他人建议获得一个int。然后,如果您的整体是订单,请查看Arrays.binarySearch().

答案 2 :(得分:1)

你真的需要尝试然后在遇到问题时在这里发帖。

但是,作为提示:对于每个双打,请查看整数列表。对于每个整数,计算double与该整数的距离。如果计算的距离小于当前最小值,则为新的最小值。

首先在纸上完成这些事情真的很有帮助。 将如何做到这一点?如果你可以解决这个问题,那么将它翻译成一个程序会更容易。

答案 3 :(得分:1)

尝试找到listOfDoubles.get(i) - anyInteger的最小值,然后存储最小差异的索引i

if (difference < min) {
    do something...
}

只是想知道我会怎么做。

答案 4 :(得分:0)

Math.round(Double d)中的{p> java.lang.Math将获取Double个对象,并将其四舍五入到最近的Long并将其绑定(.5)。

请参阅:Oracle Java Documentation